1

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)
sammy
  • 717
  • 4
  • 13
  • Your code may contain syntax errors. Consult these following links http://php.net/manual/en/function.mysql-error.php and http://php.net/manual/en/function.error-reporting.php and apply that to your code. – Funk Forty Niner Nov 04 '15 at 15:02
  • @Fred-ii- no error is there! code is true? need to write table entities too? – sammy Nov 04 '15 at 15:05
  • 1
    @Fred-ii- I have also error, this is because OP using old supported system, neither mine nor yours environment are compatible longer, I think it is because some components of the code are deprecated. ;) any I will take a look and see what I can do. – Maytham Fahmi Nov 04 '15 at 15:14
  • @maytham-ɯɐɥıλɐɯ: u always helped me bro... i think realy i must not use of sql anymore. – sammy Nov 04 '15 at 15:24
  • 1
    One thing to note is that you have a field named `date` which has special meaning - thus you ought to encase fields names in backticks.. – Professor Abronsius Nov 04 '15 at 15:24
  • @RamRaider: im in basic level of puting information to table, in this level date is a simple field and do not have any special codes... – sammy Nov 04 '15 at 15:27
  • @maytham-ɯɐɥıλɐɯ: did u found the problem? – sammy Nov 04 '15 at 15:35
  • 1
    @sajad right now i am in work doing other stuff, but check it later ;) you got my word – Maytham Fahmi Nov 04 '15 at 15:36
  • @maytham-ɯɐɥıλɐɯ: thanks :) – sammy Nov 04 '15 at 15:39
  • @maytham-ɯɐɥıλɐɯ: i need your help, are you online? – sammy Nov 05 '15 at 16:49

1 Answers1

1

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.

Community
  • 1
  • 1
Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137
  • oh that did not worked :( may problem related to my table? let me add my table to question. – sammy Nov 05 '15 at 07:03
  • i got where is problem. when i create relation in table, form returns null, i have two primary keys. but when i remove relation that works property! – sammy Nov 05 '15 at 07:13
  • 1
    Again @sajad as you can see every thing in web development is depending on each other, clean code, better structure, good var names and good database design will play a role at the final end, there will always be small problems and challenges but those will got reduced doing all that better. any way I understand that you are in your beginner phase, that is fare, but try to improve, what suggested. Good luck. – Maytham Fahmi Nov 05 '15 at 09:32
  • 1
    im happy becuase you help me always and i think i can improve my coding with your help and others :) thanks bro :) – sammy Nov 05 '15 at 09:39
  • i think just you can help me here:http://stackoverflow.com/questions/33550554/insert-to-all-cells-when-records-are-more-than-one-php – sammy Nov 05 '15 at 17:24