2

I want to auto populate my data field using php mysql. I search my data using id which is stu_id. My database is like this

stu_id s_name reg program etc

and then I add 3 new input field and want to insert it a new table named payment_all .

Here is my auto.php file

<!DOCTYPE HTML>
<head>
      <title></title>
 </head>

<body>

<!-- form to get key detail of record in database -->
<form name="form" method="POST" action="">
  Keyfield <input type="text" name="search"> <br><br>
  <input type="submit"  value="submit">
</form>


<?php

$connection = mysql_connect('localhost','root','') or die ("Couldn't connect to server."); 
$db = mysql_select_db('assignment', $connection) or die ("Couldn't select database."); 

$search=$_POST['search'];

$data = 'SELECT * FROM `stu_tbl` WHERE `stu_id` = "'.$search.'"';
  $query = mysql_query($data) or die("Couldn't execute query. ". mysql_error());
  $datas = mysql_fetch_array($query);


?>



<!-- form to display record from database -->
<form name="form" method="POST" action="search.php">
  Name: <input type="text" name="s_name" value="<?php echo $datas['s_name']?>"/> <br>
  Reg: <input type="text" name="reg" value="<?php echo $datas['reg']?>"/> <br>
  Faculty: <input type="text" name="factxt" value="<?php echo $datas['program']?>"/><br><br>
    <input type="hidden" name="stu_id" value="<?php echo $search?>"> 
    Semester: <input type="text" name="semtxt"><br>
    Payment Category: <input type="text" name="papytxt"><br>
  Total Pay:  <input type="text" name="tptxt"><br>
  <input type="submit"  value="submit"><br>
</form>

</body>

</html>

and Here is my search.php file

<?php

$connection = mysql_connect('localhost','root','') or die ("Couldn't connect to server."); 
$db = mysql_select_db('assignment', $connection) or die ("Couldn't select database."); 

    $stu_id=$_POST['stu_id'];
    $semester=$_POST['semtxt'];
    $s_name=$_POST['s_name'];
    $reg=$_POST['reg'];
    $fa_name=$_POST['factxt'];
    $pay_name=$_POST['papytxt'];
    $totalpay=$_POST['tptxt'];

$data = mysql_query("INSERT INTO payment_all 
                        VALUES(
                            NULL,
                            'stu_id',
                            '$semester',
                            '$s_name',
                            '$reg',
                            '$fa_name' ,
                            '$pay_name',
                            '$totalpay'
                            )
                    ");
  $query = mysql_query($data) or die("Couldn't execute query. ". mysql_error());

?>

<!DOCTYPE HTML>
<head>
      <title></title>
 </head>

<body>

<!--  display the changed record from database -->
  Name: <?php echo $s_name?><br>
  Reg: <?php echo $reg?> <br>
  Departyment: <?php echo $program?><br><br>

</body>

</html>

I got error. Its says Couldn't execute query. Column count doesn't match value count at row 1

Josip Ivic
  • 3,639
  • 9
  • 39
  • 57
  • 2
    [Your script is at risk for SQL Injection.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and consider using PDO, [it's not as hard as you think](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard May 12 '15 at 18:08
  • 1
    You have forgotten the `$` on the `stu_id` in the query. How many columns does the table have? You have to insert the same number of columns when you're not specifying them. – Jay Blanchard May 12 '15 at 18:09

0 Answers0