简体   繁体   中英

some data are not inserted in mysql database

here i have the php code for connection and insertion of data to the database. tfs.php :-

  <?php
     require "init.php";
$fsname = $_POST['fsname'];
$lsname =$_POST['lsname'];
$contact =$_POST['contact'];
$password =$_POST['password'];
$rent_on =$_POST['rent_on'];
$room_status =$_POST['room_status'];
$room_for =$_POST['room_for'];
$quantity =$_POST['quantity'];
$beds_status =$_POST['beds_status'];
$study_table =$_POST['study_table'];
$wifi =$_POST['wifi'];
$gizer =$_POST['gizer']; 
$city =$_POST['city'];
$area =$_POST['area'];
$college =$_POST['college'];
$mail_id =$_POST['mail_id'];
$address =$_POST['address'];
$pcode =$_POST['pcode'];
$geo =$_POST['geo'];

   $sql = "INSERT INTO `user_record`(`fsname`,`lsname` , `rent_on`,`room_status`,`city`,`area`,`college`,`room_for`,`quantity`,`beds_status`,`study_table`,`wifi`,`gizer`,`contact`,`password`,`mail_id`,`address`) VALUES ('$fsname','$lsname','$rent_on','$room_status','$city','$area','$college','$room_for','$quantity','$beds_status','$study_table','$wifi','$gizer','$contact','$password','$mail_id','$address')";

if(mysqli_query($con,$sql))
{
    echo "<br><h3> Data submitted....</h3>";
}
else
{
    echo "Error in insertation..." . mysqli_error($con);
}
 ?> 

and here is the html code :-

           <html>

           <head><title>Add info...</title></head>

           <body>

           <form action="tfs.php" method="post">
           <table>
           <tr>
            <td>First Name :</td>
           <td><input type="text" name="fsname" /></td>
          </tr>
          <td>Last Name :</td>
          <td><input type="text" name="lsname" /></td>
          </tr>
          <tr>
          <tr>
          <td>rent on :</td>
         <td><input type="text" name="rent_on" /></td>
        </tr>
         <tr>
         <td>room status :</td>
        <td><input type="text" name="status" /></td>
       </tr>
       <td>city :</td>
       <td><input type="text" name="city" /></td>
        </tr>
       <tr>
        <td>area :</td>
      <td><input type="text" name="area" /></td>
        </tr>
       <tr>
      <td>college :</td>
       <td><input type="text" name="college" /></td>
       </tr>
        <tr>
       <td>room for :</td>
        <td><input type="text" name="rent_for" /></td>
       </tr>
        <tr>
          <td>quantity :</td>
          <td><input type="text" name="quantity" /></td>
          </tr>
          <tr>
          <td>bed status :</td>
         <td><input type="text" name="bed" /></td>
           </tr>
         <tr>
         <td>study table :</td>
         <td><input type="text" name="s_table" /></td>
           </tr>
         <tr>
       <td>wifi :</td>
        <td><input type="text" name="wifi" /></td>
       </tr>
        <tr>
       <td>gizer :</td>
         <td><input type="text" name="gizer" /></td>
         </tr>
        <tr>
       <td>Contact :</td>
         <td><input type="text" name="contact" /></td>
       </tr>
            <tr>
          <td>password :</td>
            <td><input type="text" name="password" /></td>
           </tr>
          <tr>
            <td>mail id :</td>
           <td><input type="text" name="mail_id" /></td>
         </tr>
         <tr>
         <td>Address :</td>
            <td><input type="text" name="address" /></td>
         </tr>
            <tr>
             <td>postal code :</td>
                 <td><input type="text" name="pcode" /></td>
             </tr>
             <tr>
             </table> 
             <input type="submit" value="Submit Info" />
                </form>
           </body>

           </html>


the connection between database is successful but some values like pcode, geo, rent shows doesn't have default values. The error is as shown in image.

the status of connection is successful but showing the error.

here is the structure of database:- the database contain 11 fields,when i will change the default values of pcode, geo, rent. then data inserted but some field shows empty and some are NULL.

so i don't know what to do now.Give the solution of that problem.

Check your Name assigned in the HTML page relevent to $_POST in PHP.

For Example:

$room_status =$_POST['room_status'];

There are no fields in HTML page.

You forgot the some fields in your insert query sno,pcode,geo & rent ,your post should be:

$room_status =$_POST['status'];
$room_for =$_POST['rent_for'];
$beds_status =$_POST['bed'];
$study_table =$_POST['s_table'];

query:

 $sql = "INSERT INTO `user_record`(`sno`,`fsname`,`lsname` , `rent_on`,`room_status`,`city`,`area`,`college`,`room_for`,`quantity`,`beds_status`,`study_table`,`wifi`,`gizer`,`contact`,`password`,`mail_id`,`address`,`pcode`,`geo`,`rent`) VALUES (NULL,'$fsname','$lsname','$rent_on','$room_status','$city','$area','$college','$room_for','$quantity','$beds_status','$study_table','$wifi','$gizer','$contact','$password','$mail_id','$address','$pcode','$geo','rent')";

You code is open to sql injection ,use prepared statements, sanitize your variables,don't forget to secure your password

  1. Your sql is vulnerable; please convert your queries to prepared and include security measures.
  2. you're missing the pcode column in your query. When you don't include a column ie ( INSERT INTO tbl (col1,col2) VALUES ('col1','col2'); ) But, your table has a col3 and the table is set up as create table tbl (col1 id not null auto_increment, col2 varchar(5), col3 boolean) . When you insert in your current query, the SQL is left assuming that you have a default value in pcode(col3) because you declined to supply it in the column declarations.

You can try insert statement like and check values against each variable easily

$sql = "INSERT INTO `user_record` SET 
        `fsname` = '".$fsname."',
        `lsname` = '".$lsname."',
        `rent_on` = '".$rent_on."'........

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM