0

here is my big program!

note: all codes are in one page.

freinds, all thing is ok but problem is in 'while' line and when i have more than one record.

here we connect to database and fetch information about users that got service of another users.

<form name="form2" method="post" action="" accept-charset='UTF-8'>
<?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("خطا در اتصال به پايگاه داده");
$selected=mysql_select_db($db_name, $con) or die("خطا در انتخاب پايگاه داده");
mysql_query("SET CHARACTER SET  utf8");

$dbresult=mysql_query("SELECT tablesite.name,
                          tablesite.family,
                          tablesite.username,
                          tablesite.phone_number,
                          tablesite.email,
                          action.service_provider_comment,
                          action.price,
                          action.date,
                          job_list.job_name,
                          relationofaction.ind
                   FROM  $db_table
                   INNER JOIN job_list
                   on job_list.job_id=action.job_id 
                   INNER JOIN relationofaction
                   on relationofaction.ind=action.ind
                   INNER JOIN tablesite
                   on tablesite.id_user=action.service_provider_id AND action.customer_id='$id'",$con);

here prints all times that current user got service of another user. it may be 0 or n.

problem is here. when services are more that 1 and when i am trying to insert informations to table, first choose of vote and explain inserts to all cells.

each button must send information to a seprated field not all fileds!

                   while($amch=mysql_fetch_assoc($dbresult))
{?>
<?php
echo'<div dir="rtl">';
echo "نام خدمت دهنده: "."&nbsp&nbsp&nbsp".$amch["name"]." ".$amch["family"]."&nbsp&nbsp&nbsp"."شماره تماس: ".$amch["phone_number"]."&nbsp&nbsp&nbsp"."ایمیل: ".$amch["email"].'<br>'.

"شغل انجام شده: ".$amch["job_name"].'<br>'
."تاریخ انجام عملیات: ".$amch["date"].'<br>'
."هزینه ی کار: ".$amch["price"]." تومان".'<br>'
.$amch["service_provider_comment"].'<hr/>';


   echo'<label for="explain">اگر توضیحاتی برای ارائه در این باره دارید، ارائه دهید</label> <br />';
   echo'<textarea name="explain" id="explain" cols="" rows="" style="width:300 ;height:300"></textarea>'.'<br/>'; 

echo'<label for="rate">امتیاز این عملیات را ثبت نمایید: </label> <br />';
echo '<select name="vote">';
echo '<option value="عالی">عالی</option>';
echo '<option value="عالی">خوب</option>';
echo '<option value="عالی">متوسط</option>';
echo '<option value="عالی">بد</option>';
echo '</select>';
echo'<br/>';
echo '<input type="submit" name="submit" value="ارسال نظر شما"/>';

echo'<hr/>';
echo'<hr/>';
echo'</div>';

}

?>

here we say if user clicked on button, send informations to table. once again i say all thing for one record is ok, problem when occure that we have more than one record and i now problem is in 'while' but i do not know how fix this problem.

<?php
if(isset($_POST['submit']))
{ 

$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 ="UPDATE $db_table
SET 
customer_comment='" . mysql_escape_string($_POST['explain']) . "',
vote='" . mysql_escape_string($_POST['vote']) . "'
WHERE ind=ind";
$saved=mysql_query($ins );
mysql_close($con); 


echo '<script language="javascript">';
echo 'alert("نظر شما با موفقیت ثبت شد")';
echo '</script>';
echo '<script>window.location.href = "action_perfomed_agree.php";</script>';
}
?>

this is a forms of a person that have 3 forms

enter image description here

and here is my table after sending

enter image description here

as you see we have repetetive records but where customer_id=37 there is not a problem because that user hasve just a record.

tables:

tablesite:

1   id_user     int(11)
2   name    varchar(128)    utf8mb4_persian_ci
3   family  varchar(128)    utf8mb4_persian_ci      
4   email   varchar(64)     utf8mb4_persian_ci      
5   phone_number    varchar(16)     utf8mb4_persian_ci  
6   username    varchar(16)     utf8mb4_persian_ci  
7   password    varchar(32)     utf8mb4_persian_ci  
8   confirmcode     varchar(32)     utf8mb4_persian_ci  

relation

1   user_name   varchar(255)    utf8mb4_persian_ci
2   job_id  int(255)
3   comments    varchar(255)
4   user_id     int(255)

RelationOfaction

1   service_provider_id     int(20)
2   customer_id     int(20)
3   ind     int(20)

action

1   job_id  int(11)
2   service_provider_id     int(10)
3   customer_id     int(10)
4   date    date
5   price   int(255)
6   vote    varchar(255)    utf8mb4_persian_ci
7   service_provider_comment    varchar(255)    utf8mb4_persian_ci
8   customer_comment    varchar(255)
9   ind     int(10)
sammy
  • 717
  • 4
  • 13
  • @maytham-ɯɐɥıλɐɯ: will this problem solved? – sammy Nov 05 '15 at 17:53
  • 1
    every thing can be solved just to find the right problem – Maytham Fahmi Nov 05 '15 at 17:55
  • This is great and all... what is the issue? – Twisty Nov 05 '15 at 19:52
  • @Twisty: i do not know where is problem actualy! – sammy Nov 05 '15 at 20:47
  • 1
    I do not see how you are submitting the Form. I suspect all your Inputs and Submit buttons are all wrapped within the same Form. This would result in any of the submit buttons submitting all of the input fields. You either need to isolate the forms to unique forms, or link JS to the button click event such that only the specific data is submitted. – Twisty Nov 05 '15 at 21:20
  • @Twisty: yeah you got problem, complete. last button send information and all coulmn are same! – sammy Nov 05 '15 at 21:31
  • @maytham-ɯɐɥıλɐɯ: im waiting for your help... – sammy Nov 06 '15 at 11:59
  • @maytham-ɯɐɥıλɐɯ below code solved a bit of problem. i think problem starts when UPDATE and WHERE. beaxause all cells fills with las value of ind. for example if ind=1 , 3, 4 the program fills cells repetetive when ind=4. – sammy Nov 06 '15 at 13:38
  • @maytham-ɯɐɥıλɐɯ: i've added images. – sammy Nov 06 '15 at 14:44
  • Just as an aside, when you move from development to production, switch to using a non-deprecated API and prepared statements. Also, in general it's a really bad idea to have code like " FROM $db_table". You should know what your table is called. – Strawberry Nov 06 '15 at 14:45

2 Answers2

2

I would advise wrapping each portion in its own form:

<?php
$id = $fgmembersite->UserID(); 
echo "$id";
$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("خطا در اتصال به پايگاه داده");
$selected=mysql_select_db($db_name, $con) or die("خطا در انتخاب پايگاه داده");
mysql_query("SET CHARACTER SET  utf8");

$dbresult=mysql_query("SELECT tablesite.name,
    tablesite.family,
    tablesite.username,
    tablesite.phone_number,
    tablesite.email,
    action.service_provider_comment,
    action.price,
    action.date,
    job_list.job_name,
    relationofaction.ind
    FROM $db_table
    INNER JOIN job_list
    ON job_list.job_id=action.job_id 
    INNER JOIN relationofaction
    ON relationofaction.ind=action.ind
    INNER JOIN tablesite
    ON tablesite.id_user=action.service_provider_id
    AND action.customer_id='$id'", $con);
$i = 1;
while($amch=mysql_fetch_assoc($dbresult)){
    echo "<form id='form_$i' method='post' action='{$_SERVER['PHP_SELF']}' 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>'
    ."شغل انجام شده: ".$amch["job_name"].'<br>'
    ."تاریخ انجام عملیات: ".$amch["date"].'<br>'
    ."هزینه ی کار: ".$amch["price"]." تومان".'<br>'
    .$amch["service_provider_comment"].'<hr/>';
    echo '<label for="explain">اگر توضیحاتی برای ارائه در این باره دارید، ارائه دهید</label> <br />';
    echo '<textarea name="explain" id="explain" cols="" rows="" style="width:300 ;height:300"></textarea>'.'<br/>'; 
    echo '<label for="rate">امتیاز این عملیات را ثبت نمایید: </label> <br />';
    echo '<select name="vote">';
    echo '    <option value="عالی">عالی</option>';
    echo '    <option value="عالی">خوب</option>';
    echo '    <option value="عالی">متوسط</option>';
    echo '    <option value="عالی">بد</option>';
    echo '</select>';
    echo '<br/>';
    echo '<input type="submit" name="submit" value="ارسال نظر شما"/>';
    echo '<hr/>';
    echo '<hr/>';
    echo '</div>';
    echo "</form>\r\n";
    $i++;
}
?>

You will find a number of little fixes in this code. This will result in a number of forms, each with a unique ID, posting to the same place.

Strawberry
  • 33,750
  • 13
  • 40
  • 57
Twisty
  • 30,304
  • 2
  • 26
  • 45
  • i perfomed your way. it is good but i think there is annother more problem. here: $selected=mysql_select_db($db_name, $con) or die("خطا در انتخاب پايگاه داده"); $ins ="UPDATE $db_table SET customer_comment='" . mysql_escape_string($_POST['explain']) . "', vote='" . mysql_escape_string($_POST['vote']) . "' WHERE ind=ind"; when i change this line to WHERE action.ind=relationofaction.ind ,,, nothing prints, if can fix this problem, there is not repetetive records any more. do you know where is problem here? – sammy Nov 05 '15 at 22:22
  • all of buttons have last ind value – sammy Nov 05 '15 at 22:27
  • 2
    There WHERE clause does not make sense. Where is `ind` defined? Did you mean `$ind`? If so, where does it's value come from? – Twisty Nov 06 '15 at 01:44
  • no, ind is a column in action table and also in relationofaction table. is auto increament. is that false? is need to use of mysql_query for select? – sammy Nov 06 '15 at 07:33
  • 1
    As you have it now, the WHERE is looking for `ind` within column `ind`, when it should be evaluating an Integer. `WHERE ind =1` or something of that nature right? – Twisty Nov 06 '15 at 08:04
  • yes yes its quite true. ind=1 sends to column with value-->1. or ind=5 sends where has value-->5. – sammy Nov 06 '15 at 10:39
  • images added. please see them. – sammy Nov 06 '15 at 14:43
  • Then you will need to change `WHERE ind=ind` to something like `WHERE ind=$ind`, and `$ind` will need to be the proper value. I cannot discern what `$ind` should be from the code you have provided. – Twisty Nov 06 '15 at 16:09
1

With respect to @Twisty answer, I would like to come with my final solution to your submission problem.

First of all we need to define which raw to be updated, therefore you need to tell your update statement which ind to be updated.

To do that we need to pass ind in out submission form to out update statement, we added a hidden input field and pass out ind value like this.

<input type="hidden" name="ind" value="' . $amch["ind"] . '">;

Next we need to fetch the value and update our data by adding following:

($_POST['ind'])

So the final solution will look like this:

echo '</select>';
echo '<input type="hidden" name="ind" value="' . $amch["ind"] . '">'; //new line
echo '<br/>';
echo '<input type="submit" name="submit" value="ارسال نظر شما"/>';
echo '<hr/>';
echo '<hr/>';

and in your update statement:

$ins = "UPDATE $db_table
        SET
        customer_comment='" . mysql_escape_string($_POST['explain']) . "',
        vote='" . mysql_escape_string($_POST['vote']) . "'
        WHERE ind=".($_POST['ind']); // to define the ind value

And wala, it works.

NOTE:
Just be a ware of, I used the updated code of @Twisty and the final solution on it.

Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137