简体   繁体   中英

Sending information to database SQL PHP

i have a form to send to table. there is two drop down list. first has information about job of current user that logged in and second one fetch all usernames of users. a field for date a field for price and a field for comment. so my code is here:

this code get all information. submit.action.php

 <form name="form5" method="post" action="send_action.php" >


<div dir="rtl">         
                <?php

   $db_host = 'localhost';
   $db_name= 'site';
   $db_table= 'job_list';
   $db_user = 'root';
   $db_pass = '';

  $user=$fgmembersite->UserNameOfUser();

  $con = mysql_connect($db_host,$db_user,$db_pass) or die("خطا در اتصال به پايگاه داده");
  $selected=mysql_select_db($db_name, $con) or die("خطا در انتخاب پايگاه داده");
 mysql_query("SET CHARACTER SET  utf8");

 $dbresult=mysql_query("SELECT job_list.job_id,
                               job_list.job_name,
                       tablesite.username
                   FROM  $db_table
          INNER JOIN relation
          on job_list.job_id=relation.job_id
          INNER JOIN tablesite
          on relation.user_id=tablesite.id_user AND tablesite.username='$user'",$con);
 echo'* خدمتی که ارائه داده اید: ','<br/>';                    
 echo '<select name="job" dir="rtl">';

 while($amch=mysql_fetch_assoc($dbresult))
 {
 echo '<option value="'.$amch['job_id'].'">'.$amch['job_name'].'</option>';
 }
 echo '</select>'; ?><br/>
 </div>     
 <!--************************************************************** --> 
 <div dir="rtl">    
 <?php  
 $db_host = 'localhost';
 $db_name= 'site';
 $db_table2= 'tablesite';
 $db_user = 'root';
 $db_pass = '';

 $con2 = mysql_connect($db_host,$db_user,$db_pass) or die("خطا در اتصال به پايگاه داده");
 $selected=mysql_select_db($db_name, $con2) or die("خطا در انتخاب پايگاه داده");
 mysql_query("SET CHARACTER SET  utf8");

 $dbresult=mysql_query("SELECT *
                              FROM  $db_table2",$con2);
 echo'* نام کاربری که به او خدمت داده اید: ','<br/>';                      
     echo '<select name="users" dir="rtl">';

     while($amch=mysql_fetch_assoc($dbresult))
      {
   echo '<option value="'.$amch['id_user'].'">'.$amch['username'].'</option>';
      }
   echo '</select>'; ?><br/>



   <label for='date' >* تاریخ عملیات:</label><br/>
   <input type='text' name='date' id='date' value='' maxlength="11" placeholder="1394/1/1" /><br/>
   <label for='price' >* هزینه کار:</label><br/>
   <input type='text'  dir="rtl" name='price' id='price' value='' maxlength="50" placeholder="54000"/><br/>

   <label for='textaria' >توضیحات:</label><br/>
   <textarea name="textaria" cols="" rows=""></textarea><br/>
   <input name="submit" type="submit" value="ثبت عملیات" />
   </div>
    </form>
  </span>
  </div>

and in this page we send information to database but nothing sends!:

 <?php
 require_once("./include/membersite_config.php");
 ?>
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 <html xmlns="http://www.w3.org/1999/xhtml">
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 <title>ارسال عملیات</title>
 </head>

 <body>

 <?php
 $id=$fgmembersite->UserID(); 
 echo "$id"; ?>

 <?php
 $db_host = 'localhost';
 $db_name= 'site';
 $db_table= 'action';
 $db_user = 'root';
 $db_pass = '';


 $con = mysql_connect($db_host,$db_user,$db_pass) or die("خطا در اتصال به پايگاه داده");

 mysql_query("SET NAMES 'utf8'", $con);
 mysql_query("SET CHARACTER SET 'utf8'", $con);
 mysql_query("SET character_set_connection = 'utf8'", $con);


 $selected=mysql_select_db($db_name, $con) or die("خطا در انتخاب پايگاه داده");
 $ins = "INSERT INTO $db_table
     (service_provider_id,customer_id,date,price,job_id,service_provider_comment)
     VALUES ('$id',
             '" . mysql_escape_string($_POST['users']) . "',
             '" . mysql_escape_string($_POST['date']) . "',
             '" . mysql_escape_string($_POST['price']) . "',
             '" . mysql_escape_string($_POST['job']) . "',
             '" . mysql_escape_string($_POST['textaria']) . "')";
$saved=mysql_query($ins );
mysql_close($con); 

?>

</body>
</html>

note: above code give all value of dropdown list and text boxes and textarea and should send them to databasetable.

note: fg_membersite included by:

      function UserID()
    {
          return isset($_SESSION['user_id'])?$_SESSION['user_id']:'';
    }

my table is as this:

    job_id                   int(11)
    service_provider_id      int(10)
    customer_id              int(10)
    date                     int(50)
    price                    int(255)
    vote                     varchar(255)
    service_provider_comment varchar(255)
    customer_comment         varchar(255)

Introduction

After looking around in your code, I have debugged your code and have localized the problem. And there are few puzzles in the problem.

I have in my test environment created your code scenario.

Your main form posts the submitted content over to send_action.php .

The problem

For debugging, I have in send_action.php page and after $selected = .... line added var_dump($_POST); . This is see where ever if the submitted content is received at all?

It shows that Job is for some reason is not posted and your ($_POST['job']) with error message Undefined index and since this error happens inside your INSERT statement. Therefore nothing get inserted.

The solution

I have checked Job and it was empty, I have added a option called default value echo '<option value="0">default job</option>'; , this is to ensure if your job list is empty under testing environment then some thing should be submitted otherwise it will returns null and gives you the problem.

echo '<select name="job">';
echo '<option value="0">default job</option>';
while ($amch = mysql_fetch_assoc($dbresult))
{
    echo '<option value="' . $amch['job_id'] . '">' . $amch['job_name'] . '</option>';
}

So far so good.

I have tested injecting manually data in your insert method and it works fine.

Finally:

After putting default value in job now every thing is working fine and your form is submitting and saving to database.

Conclusion

  1. It is important to switch over to MySQLi or PDO
  2. Variable naming should be improved
  3. Make use of function to reducing mass repetitive codes like mysql_escape_string or database connection, make central page and include the connection (I have an answer showing a min project using PDO you can take a look and get inspiration here )
  4. Better and more clear code and data structure.

Note

The solution is based on what is presented in your code as is, I have had no chance to test in depth or against a final running database. I have created a table for inserting the data, I have also got your old version database var_dump which helped also.

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