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); 
 ?>

I want to save the $in variables value in the query but not able to save it. At time of insertion it saves the value 0 not the actual value.

DanielBarbarian
  • 5,093
  • 12
  • 35
  • 44
Gundeep
  • 21
  • 7
  • You want to save all the card numbers you got from the first query ? – Pardeep Poria Nov 06 '16 at 07:01
  • outside of the while loop try to initiate the $in variable by $in = 0, echo out the value of $in after $in++, see if its is actually getting increment.@gundeep – Parth Mahida Nov 06 '16 at 07:03
  • remove the $in++; inside the while loop – Vikas Umrao Nov 06 '16 at 07:06
  • @parthmahida if i make $in=0 then it always take the value after increment the same we intialised before. – Gundeep Nov 06 '16 at 07:15
  • @parthmahida the highest value in my database gets saved after incrementing my 1 – Gundeep Nov 06 '16 at 07:17
  • stop using `mysql_*()` functions, read the documentation of `mysql_connect()` recently? – Xorifelse Nov 06 '16 at 07:19
  • Try to concatenate the variable like.. $in .= $row['card_no']; then echo $in outside the loop.. if not needed then remove $in++ – zed Nov 06 '16 at 07:21
  • You are assigning the value to $in inside the loop so it will not get zero, but the value of $row['card_no'] will be assigned to $in, you are just incrementing it by 1, so if the value of the record $row['card_no'] is 2, the $in will 4 because there is only one record you are returning through query.@gundeep – Parth Mahida Nov 06 '16 at 07:26
  • This query has a whacking great SQL injection vulnerability in it, don't use it! Upgrade to a newer database library and use parameter binding. There is some [good material here](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php). – halfer Nov 06 '16 at 09:49

1 Answers1

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>

this is the actual code in this input box the value is incrementing help me save the value of this input box into another file which is database file.

Gundeep
  • 21
  • 7
  • what are you trying so far....... Are you trying to get last inserted card number first then increment? If so, then why you are using loop to get that value. If i'm wrong, then explain more your situation, may be problem is not complicated as assumed. – zed Nov 06 '16 at 07:59
  • try query Like this: "SELECT MAX(card_no) AS card_no FROM list1". it will return maximum number from the list e.g. max card not is 6, then increment by 1 and insert into your new query. No need to use loop here. – zed Nov 06 '16 at 08:19
  • nothing is changed in the database after using your code @zed – Gundeep Nov 06 '16 at 08:50