send id_user of choosed user to database

2019-08-10 19:58发布

here is name and informations of my users:

this is my code:

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

/* echo "$id"; */


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


$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 tablesite.id_user,
                              tablesite.name,
                              tablesite.family,
                              tablesite.username,
                              tablesite.phone_number,
                              tablesite.email
                       FROM  $db_table",$con);
   $i = 1;

                       while($amch=mysql_fetch_assoc($dbresult))

{?>
  <?php

echo "<form name=f1 id='form_$i' method='post' action=submit_action.php accept-charset='UTF-8'>\r\n";
echo'<div dir="rtl">';
echo "نام خدمت دهنده: "."&nbsp&nbsp&nbsp".$amch["name"]." ".$amch["family"]."&nbsp&nbsp&nbsp"."شماره تماس: ".$amch["phone_number"]."&nbsp&nbsp&nbsp"."ایمیل: ".$amch["email"].'<br>';
echo '<input type="hidden" name="id_user" value="' . $amch["id_user"] . '">';
echo '<input type="hidden" name="name" value="' . $amch["name"] . '">';
echo '<input type="hidden" name="family" value="' . $amch["family"] . '">';
echo '<input type="hidden" name="phone_number" value="' . $amch["phone_number"] . '">';
echo '<input type="hidden" name="email" value="' . $amch["email"] . '">';
echo '<input type="submit" name="choose" value="انتخاب مشتری"/>';echo'<hr/>';
echo'<hr/>';
echo'</div>';
echo "</form>\r\n";
    $i++;
}
?>

for example i choosed one buddy that you can see in below picture, from appeared list:

enter image description here

and his information sends to second page:

second page

all things property works till this step. in next step i want to send id_user of selected user. as you see in picture, id_user of current user is 34. bellow code is all codes of second page.

problem is where i want id_user (for this user 34) to database but i get following notice:

( ! ) Notice: Undefined index: id_user in C:\wamp\www\source\submit_action.php on line 339

line 339: VALUES ('$id', '" . $_POST['id_user'] . "', etc...

        <form name="form5" method="post" action="" >


<div dir="rtl"> 

                    <?php


$username=$fgmembersite->UserNameOfUser();
   $db_host = 'localhost';
   $db_name= 'site';
   $db_table= 'job_list';
   $db_user = 'root';
   $db_pass = '';



$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='$username'",$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"> 


            <label for='date' >* تاریخ عملیات:</label><br/>
            <?php

echo'<select dir="rtl" name="day">';
for($day=1;$day<=31;$day++)
            {


            echo '<option value="' . $day . '">' . $day . '</option>';

            }
echo'</select>';


echo'<select dir="rtl" name="month">';
for($month=1;$month<=12;$month++)
            {


            echo '<option value="' . $month . '">' . $month . '</option>';

            }
echo'</select>';

            echo'<select dir="rtl" name="year">';
            for($year=1388;$year<=1410;$year++)
            { 
            echo '<option value="' . $year . '">' . $year . '</option>';

            } 

echo'</select>';

?>          


            <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>


            <?php
if(isset($_POST['choose']))
{
$id_user =$_POST['id_user'];
$name=$_POST['name'];
$family=$_POST['family'];
$phone_number=$_POST['phone_number'];
$email=$_POST['email'];
echo "$name", " " , "$family", '<br/>' , "$phone_number", '<br/>' , "$email",'<br/>' , "$id_user";
}

?>  

</form>



<?php

if(isset($_POST['submit']))
{ 
$date = $_POST['year'].'-'.$_POST['month'].'-'.$_POST['day'];

$id=$fgmembersite->UserID(); 

$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',
                 '" . $_POST['id_user'] . "',
                 '$date',
                 '" . mysql_escape_string($_POST['price']) . "',
                 '" . mysql_escape_string($_POST['job']) . "',
                 '" . mysql_escape_string($_POST['textaria']) . "')";
$saved=mysql_query($ins );
mysql_close($con); 

/*echo '<script language="javascript">';
echo 'alert("عملیات با موفقیت ثبت شد")';
echo '</script>';
echo '<script>window.location.href = "choose_user.php";</script>';
*/

}
?>

after if(isset($_POST['submit'])), cant recognize $id_user. i think problem is here.

标签: php
2条回答
一纸荒年 Trace。
2楼-- · 2019-08-10 20:21

This is because you have more than one form and if you do not forward your value to second form, the value will be flushed (become NULL, said in other words it does not re-send the id_user automatically to a second form).

First of all you need to declare and retrieve the id_user on your new form, by adding following line in the beginning of your submit_action.php file:

$id_user = $_POST['id_user'];

So your code will look like this:

.....
<div dir="rtl">

    <?php
    $id_user = $_POST['id_user'];

    $username = $fgmembersite->UserNameOfUser();
etc....

Here next you can either pass the variable inside a hidden input, by adding following line before your form submit button so it looks like this:

.....
<input type="hidden" name="id_user" value="<?php echo $id_user ?>">

<input name="submit" type="submit" value="ثبت عملیات"/>

etc....

Or you can pass the variable directly in your statement so it looks like this:

.....
VALUES ('$id',
                 '" . $id_user . "',
                 '$date',
                 '" . mysql_escape_string($_POST['price']) . "',
etc....

That should solve the trick.

Note: After chatting with OP the answer is updated and further improved.

查看更多
唯我独甜
3楼-- · 2019-08-10 20:44

$_POST['id_user'] is unknown, because it doesn't send with the second form. it means id_user don't send to the block: if(isset($_POST['submit'])) {...} so it is Undefined index.

查看更多
登录 后发表回答