Create a new list and add data using the checkboxes

I am trying to create a list in which selected items are stored. The way this happens is that you give the list a name, and then choose which items you want to add to the list. First, I create a list with a name. Then, the created list receives the primary key of automatic increase. I need to use the listPK primary key to add items to the correct list. My problem is that I cannot get the value from $ listRes. But if I hard code the value. For example, specify $ listRes = 1. It works if there is a list with primary key 1. However, I need this to get the primary key value from a newly created list. So I try this:

$listPK = "SELECT listPK from list WHERE name = '$name';";
$listRes = mysql_query($listPK);

I can’t get it right. I would appreciate any tips, advice or any thoughts on how to approach this.

table

I have three tables. enter image description here

This is my code:

if (isset($_POST['CreateList'])) {
    $name = $_POST['name'];
    mysql_query("INSERT INTO list (`name`) VALUES ('$name);");
}

    $listPK = "SELECT listPK from list WHERE name = '$name';";
    $listRes = mysql_query($listPK);

    //It is working creating a new list, but it is not working listing out the primary key afterwards. 

    foreach ($_POST['addUser'] as $check) {
        mysql_query("INSERT INTO inList (`user`, `list`) VALUES ('$check', '$listRes');");
    }
+4
source share
1 answer

Well first

$listPK = "SELECT listPK from liste WHERE name = '$name';";

should look like

$listPK = "SELECT listPK from List WHERE name = '$name'";

like rawk said second

You choose from liste, but the name of your table in the chart Listwith capital L make sure that

and still not debugging like echo$ listPK

EDIT

$listPK = "SELECT listPK from list WHERE name = '$name';";
    $listRes = mysql_query($listPK);
Problem

is that you are not accepting the primary key value in your $ listRes, as you said you were getting resource # 19 something

you need to accept the primary u value to do something like this

while ($row = mysql_fetch_array($listRes))
{
foreach ($_POST['addUser'] as $check) {
    mysql_query("INSERT INTO inList (`user`, `list`) VALUES ('$check', '$row['listPK']');");
}
}

but I really want to say that if you are doing the project yourself, go to mysqli or PDO

, :)

+1

Source: https://habr.com/ru/post/1531771/


All Articles