2016-11-06 97 views
0
<?php 
//connecting to the database 
define('DB_HOST', 'localhost'); 
define('DB_NAME', 'visitor_list'); 
define('DB_USER','root'); 
define('DB_PASSWORD',''); 
$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to   MySQL: " . mysql_error()); $db=mysql_select_db(DB_NAME,$con) or die("Failed to  connect to MySQL: " . 
mysql_error()); //inserting Record to the database 
$dat1 = date("d"); 
$moth1 = date("m"); 
$year1 = date("Y"); 
$sql2 = 'SELECT * FROM list1 ORDER BY card_no DESC LIMIT 1;'; 
$retval = mysql_query($con,$sql2); 
while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) 
{ 
/*echo "{$row['card_no']}";*/ 
$in= $row['card_no']; 
$in++; 
} 

$ap_ty = $_POST['ap_ty']; 
$per = $_POST['per']; 
$no_vis = $_POST['no_vis']; 
$nm_vis = $_POST['nm_vis']; 
$co_nm = $_POST['co_nm']; 
$ad = $_POST['ad']; 
$po = $_POST['po']; 
$ty_vis = $_POST['ty_vis']; 
$met_nm = $_POST['met_nm']; 
$dep = $_POST['dep']; 
$des = $_POST['des']; 
$por_met = $_POST['por_met']; 
$vad = $_POST['vad']; 

if(!isset($_POST['submit'])){ 
$query = "INSERT INTO list1 (card_no,d1,m1,y1,app_ty,no_per,area_vis,nm,com_nm,add1,pho,ty1_vis,met_with,depart,desi,pur_meet,vad_up) VALUES  ('$in','$dat1','$moth1','$year1','$ap_ty','$per','$no_vis','$nm_vis','$co_nm','$ad','$po','$ty_vis','$met_nm','$dep','$des','$por_met','$vad')";} 
$result = mysql_query($query); 
if($result) 
{ 
echo '<script type="text/javascript"> 
alert("Your Deatils has been Submitted"); 
</script> '; 
header('location:index.php'); 
} 
else 
{ die('Error: '.mysql_error($con)); } 
mysql_close($con); 
?> 

我想將$in變量值保存在查詢中但無法保存。在插入時它保存值0而不是實際值。如何將變量值保存在PHP中的循環之外?

+0

你想保存你從第一個查詢中獲得的所有卡號嗎? – Poria

+0

while循環之外嘗試通過$ in = 0啓動$ in變量,在$ in ++之後回顯$ in的值,看看它是否實際上正在增加。@ gundeep –

+0

刪除$ in ++;在while循環內 –

回答

0
<input type="text" class="form-control" name="vis_id" value=" <?php 
          $dbhost = 'localhost'; 
          $dbuser = 'root'; 
          $dbpass = ''; 
          $conn = mysql_connect($dbhost, $dbuser, $dbpass); 
          if(! $conn) 
           { 
           die('Could not connect: ' . mysql_error()); 
           } 
          $sql = 'SELECT * FROM list1 ORDER BY card_no DESC LIMIT 1;'; 
          mysql_select_db('visitor_list'); 
          $retval = mysql_query($sql, $conn); 
          if(! $retval) 
          { 
           die('Could not get data: ' . mysql_error()); 
          } 
          while($row = mysql_fetch_array($retval, MYSQL_ASSOC)) 
          { 
           /*echo "{$row['card_no']}";*/ 
           $in= $row['card_no']; 
           $in+ 
           $test = $in; 
          } 
          mysql_close($conn); 
         ?>" disabled> 

這是在此輸入框中的值遞增 幫我保存這個輸入框的值到另一個文件是數據庫文件的實際代碼 。

+0

@parthmahida 檢查上面的代碼 – Gundeep

+0

@Vikas Umrao檢查上面的代碼 – Gundeep

+0

@RAUSHAN KUMAR檢查上面的代碼 – Gundeep

相關問題