0

i'm working on a php script wherein i must add certain score value at each row. I was able to display all the rows but i'm not sure on how would I able to store each of the given score in a variable and what query should I make to add all of them.

Here's my code

    <?php
echo '<html>';
?>
<body>
<table border=0 cellspacing=0 cellpadding=0>
<?php
$connect = mysql_connect('localhost', 'root', '');
$db = 'labs';
$tb = 'comments';
$seldb = mysql_select_db($db, $connect); 
echo '<form method="POST" action="..'.$_SERVER["PHP_SELF"].'">';
$query = mysql_query('SELECT com_id, comments FROM comments ORDER BY com_id ASC');
while($i = mysql_fetch_assoc($query)) {
    echo'<tr><td>'.$i['comments'].'</td>';
    echo'<td><select name="score" id="score" size="1">
    <option value="5">5</option>
    <option value="10">10</option>
    <option value="15">15</option>
    <option value="20">20</option>
    <option value="25">25</option>
    <option value="30">30</option>
    <option value="35">35</option>
    <option value="40">40</option>
    <option value="45">45</option>
    <option value="50">50</option>
    </select></td></tr>';
    echo'<br>';
}
echo'<input type="submit" name="submit" value="submit">';
echo'</form>';

if(isset($_POST['submit'])) {
    //not sure if all the scores will be stored in here.
    $id = $_POST['id'];
    $query = mysql_query('insert query here');
}
?>

</table>
</body>
</html>

any suggestions are appreciated. Thanks in advance.:D

5
  • 1
    I not really understud your question. I guess you want access the select later in your script to save it back, right? Add a counter to your while, or use for and add the value to the select: <select name="score_"' + $x + '"> Commented Mar 3, 2014 at 15:05
  • Do you want to add a score and display it or store it back in the database? If you want to update the database why don't you just do update table_name set field_name = field_name + 5? Commented Mar 3, 2014 at 15:07
  • 1
    For the record, you made a typo in your option tags: valyue, should be value. Commented Mar 3, 2014 at 15:08
  • i fixed it. so.example it returned 5 rows. the first want i wanna add 5. the other, 10, etc. Then in one submit, i want all of those rows i inserted a score be updated. Commented Mar 3, 2014 at 15:10
  • print_r($_POST) to see what values are beeing sent. Then you will see that you need to identify each row of data (beeing each new $i), so add a new field (<hidden> or name="array[]", stackoverflow.com/questions/2793891/…) and work from there. For the SQL, you could iterate over all ids and use the UPDATE syntax dev.mysql.com/doc/refman/5.0/en/update.html Commented Mar 3, 2014 at 15:10

4 Answers 4

0

I think you need the id of each changed row (maybe as a hidden field for each row. Then just do a loop through all received rows and UPDATE each one.

You might also want to change all of your form field names to use the array format. This way it's easier to make your PHP loop throught them.

Sample row:

echo'<tr><td>'.$i['comments'].'</td>';
echo'<td><select name="score['.$i["id"].']" id="score" size="1">
    <option value="5">5</option>
    <option value="5">10</option>
    <option value="5">15</option>
    <option value="5">20</option>
    <option value="5">25</option>
    <option value="5">30</option>
    <option value="5">35</option>
    <option value="5">40</option>
    <option value="5">45</option>
    <option value="5">50</option>
    </select></td></tr>';

Now just loop through the $_POST["score"] array and use the appropriate ID for your update.

foreach($_POST["score"] as $id => $value{
    // ESCAPE your db values!!!!!
    // query stuff with $value and $id
}

Also keep in Mind

  • mysql is deprecated! Use mysqli
  • Escape anything from outside sources like $_POST before use in SQL
Sign up to request clarification or add additional context in comments.

Comments

0

You just needs to make an array of your drop down box like below,

while($i = mysql_fetch_assoc($query)) {
echo'<tr><td>'.$i['comments'].'</td>';
echo'<td><select name="score[".$i['com_id']."]" id="score" size="1">
<option valyue="5">5</option>
<option valyue="5">10</option>
<option valyue="5">15</option>
<option valyue="5">20</option>
<option valyue="5">25</option>
<option valyue="5">30</option>
<option valyue="5">35</option>
<option valyue="5">40</option>
<option valyue="5">45</option>
<option valyue="5">50</option>
</select></td></tr>';
echo'<br>';
}

and you can access it for all of your comments

Comments

0
<option valyue="5">50</option> 

should be

 <option value="5">50</option> 

To send the value of a comment to database you need to add a ID of the comment

you should loop something like this.

 $query = mysql_query('SELECT com_id, comments FROM comments ORDER BY com_id ASC');

 while($i = mysql_fetch_assoc($query)) {

    echo '<form method="POST" action="..'.$_SERVER["PHP_SELF"].'">';

    echo '<input type="hidden" name="id" value="'.$i['com_id'].'">';

    echo'<tr><td>'.$i['comments'].'</td>';

    echo'<td><select name="score" id="score" size="1">
    <option value="5">5</option>
    <option value="5">10</option>
    <option value="5">15</option>
    <option value="5">20</option>
    <option value="5">25</option>
    <option value="5">30</option>
    <option value="5">35</option>
    <option value="5">40</option>
    <option value="5">45</option>
    <option value="5">50</option>
    </select></td></tr>';

    echo'<br>';

    echo'<input type="submit" name="submit" value="submit">';

    echo'</form>';
}

8 Comments

so the query should also be under the while loop?
You want each comment to have a score right? the code above works for multiple comments that need a independent score. you need a code to recieve id,score so you can update the score on that specific comment.
so how about the query? should it also be under the loop?
no. it has to be outside and add a trigger example. <?php if(isset($_POST)){ $sql = "SQL UPDATE"; if(mysql_query($sql)) { echo 'Comment Updated'; } ?>
so will it go like this?
|
0

I guess the easiest way for you is the following (a mix of the other solutions and comments):

<?php
echo '<html>';
?>
<body>
<table border=0 cellspacing=0 cellpadding=0>
<?php
$x = 0;
$connect = mysql_connect('localhost', 'root', '');
$db = 'labs';
$tb = 'comments';
$seldb = mysql_select_db($db, $connect); 
echo '<form method="POST" action="..'.$_SERVER["PHP_SELF"].'">';
$query = mysql_query('SELECT com_id, comments FROM comments ORDER BY com_id ASC');
while($i = mysql_fetch_assoc($query)) {
    $x++;
    echo'<tr><td>'.$i['comments'].'</td>';
    echo'<td><select name="score_'.$x.'" id="score" size="1">
    <option value="5">5</option>
    <option value="10">10</option>
    <option value="15">15</option>
    <option value="20">20</option>
    <option value="25">25</option>
    <option value="30">30</option>
    <option value="35">35</option>
    <option value="40">40</option>
    <option value="45">45</option>
    <option value="50">50</option>
    </select></td></tr>';
    echo'<br>';
}
echo'<input type="submit" name="submit" value="submit">';
echo'</form>';

if(isset($_POST['submit'])) {
    //not sure if all the scores will be stored in here.
    $id = $_POST['id'];

    for($y = 0;$y <= $x; $y++)
    {
        //This query is no sql injection save. Please add filters for productive uses!!
        $query = mysql_query('UPDATE table_name SET score = '.$_POST["score_".$y].' WHERE id='.$id);
    }
?>

</table>
</body>
</html>

Code is no tested!

2 Comments

Please either remove the actual SQL query containing SQL injection flaws or add a filter.
Yeah that's true. Sorry for my mistake. I added a hint to the solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.