1

I have this code

while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
    $rName = $row['abb'];
    echo $row['name'];
?>
<html>
    <select id="<?php echo $rName;?>">
        <option value="Yes">Enabled</option>
        <option value="No">Disabled</option>
    </select>
<?php
    echo '<br>';
}
    echo '<br>';
    echo '<input type="submit" name="submit"/>';
    echo '</form>';

So as you can see, Everytime I have a new $row['name'] its going to create a new selector with the name of $row['abb']

How do I do the update the table priv and put the value from that selector into the row with the same name as the selector..

For instance, if I had two $row['name'] it would create two $row['abb'] One would be named op2, the other would be named op3

In my priv table I have rows op2-op24 . So How could I do this so it know to update the correct row with that value?

UPDATE 1**

I tried this

                if(isset($_POST['submit'])){
            $userIDi = $user['ident'];
            $op2 = $_POST['op2'];
            $op3 = $_POST['op3'];
            $op4 = $_POST['op4'];
            $op5 = $_POST['op5'];
            $op6 = $_POST['op6'];
            $op7 = $_POST['op7'];
            $op8 = $_POST['op8'];
            $op9 = $_POST['op9'];
            $op10 = $_POST['op10'];
            $op11 = $_POST['op11'];
            $op12 = $_POST['op12'];
            $op13 = $_POST['op13'];
$stmt=$con->prepare("UPDATE priv SET op2 = $op2, op3 = $op3, op4 = $op4, op5 = $op5,
 op6 = $op6, op7 = $op7, op8 = $op8, op9 = $op9, op10 = $op10, op11 = $op11, op12 = $op12, op13 = $op13 WHERE ident = :ID");
$stmt->bindparam(":ID", $userIDi);
$stmt->execute();

Doing that gave me this error

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' op4 = , op5 = , op6 = , op7 = , op8 = , op9 = , op10 = , op11 = , op12 = , op' at line 1' in /nfs/c11/h05/mnt//html/orderpage.php:204 Stack trace: #0 /nfs/c11/h05/mnt/2080/html/orderpage.php(204): PDOStatement->execute() #1 {main} thrown in /nfs/c11/h05//html/orderpage.php on line 204

Kmiles1990123
  • 189
  • 2
  • 12
  • give the select a name attribute and assign a variable to a POST/GET array with a form. I don't see the opening form tag, so I don't know which method you're using here. – Funk Forty Niner Feb 18 '16 at 19:20
  • Can you give me an example? I understand how to do $new = $_POST['op2'];.... But I might not have all 24 ops so what if I only have too, Do I still have to write all the code out for 24 of them? – Kmiles1990123 Feb 18 '16 at 19:21
  • then you'd need to use a `foreach` with a key value and check on empty in the array. – Funk Forty Niner Feb 18 '16 at 19:22
  • @Fred-ii- Could you please give me an example with the code I provided. – Kmiles1990123 Feb 18 '16 at 19:24
  • 1
    *I'm still thinking here*, and TBH, I'm having a bit of trouble now wrapping my head around it. The part that's confusing me is the ` – Funk Forty Niner Feb 18 '16 at 19:35
  • ...then `` and/or use the rows value in the ` – Funk Forty Niner Feb 18 '16 at 19:36
  • @Fred-ii- Please read my Updates. – Kmiles1990123 Feb 18 '16 at 19:57
  • [Your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Jay Blanchard Feb 18 '16 at 20:02
  • @JayBlanchard Not if I cant get it to work at all, lol. – Kmiles1990123 Feb 18 '16 at 20:03
  • 1
    For each variable you are using to perform an update you need to surround it with quotes, i.e `op4 = '$op4'`. Even better would be to use [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [PDO](http://php.net/manual/en/pdo.prepared-statements.php). – Jay Blanchard Feb 18 '16 at 20:03
  • @JayBlanchard I did what you said and it worked partially. It only saved op2, It didnt save op3 – Kmiles1990123 Feb 18 '16 at 20:05
  • Have a look at this answer http://stackoverflow.com/a/35463521/ which is what I had in mind that could help you. Pretty sure you can make use of that one and is dynamic. – Funk Forty Niner Feb 18 '16 at 20:06
  • You did it for each variable? – Jay Blanchard Feb 18 '16 at 20:07
  • @JayBlanchard Yes, I did it for each one. – Kmiles1990123 Feb 18 '16 at 20:07
  • If you `print_r($_POST);` what do you get back? – Jay Blanchard Feb 18 '16 at 20:08
  • I got this `Array ( [op2] => Yes [op1] => No [submit] => Submit )` – Kmiles1990123 Feb 18 '16 at 20:10
  • Wait, I just realised Why is it using op1? wth – Kmiles1990123 Feb 18 '16 at 20:10
  • Have a look at the markup you generated. – Jay Blanchard Feb 18 '16 at 20:13
  • Yep, I just got it working. Now it is saving correctly. Could I ask you one more question. I made another post about it earlier but I CANNOT for life of me get this right. How do I make it so the selector is auto selected on either enabled or disabled based on what the value is from that other priv. – Kmiles1990123 Feb 18 '16 at 20:16
  • AS you can see in the above code, I am creatinging the selectors in a while statment for the table access. The values however once submitted get saved into the table priv.. So how do I make this selector "an a while for table access" to be able to read the corresponding value from the table priv – Kmiles1990123 Feb 18 '16 at 20:19
  • Everytime I use 2 while statments the other one cancels the other out. – Kmiles1990123 Feb 18 '16 at 20:20
  • @Fred-ii- could you check ou my other post and see if you could help me http://stackoverflow.com/questions/35487194/while-statement-not-echoing-out-info?noredirect=1#comment58670533_35487194 – Kmiles1990123 Feb 18 '16 at 20:24

0 Answers0