A query: [PHP - MySQLI]

-2

Dear

I want to know, I must add a record by POST.

for example

if($_POST==add){
 $id= $_POST['id'];
 $name = $_POST['name'];
 $userID = $_POST['userID'];

$sql = "INSERT INTO table('id', 'name', 'userID') VALUES (NULL, "Jorge", "NULL o $userID");

}
    
asked by Diego Sagredo 27.10.2016 в 22:29
source

2 answers

0

Good morning,

First of all, what code is what you have developed?

There are several syntax errors that can make it not run:

  • the comparison of if if($_POST==add){ add is a constant? otherwise, that generates error. If you are looking for the value of a button it should be something like if($_POST['nombrecampo']=='add') .
  • In the insert, if Jorge is an explicit value, you should go inside quotes, if you want to concatenate the values of the post, you would have to do something like:

    $sql = "INSERT INTO table('id', 'name', 'userID') VALUES ('".$_POST['id']."', '".$_POST['name']."','".$_POST['userID']."'); or

    $sql = "INSERT INTO table('id', 'name', 'userID') VALUES ('$variable1',$variable2',$variable3');

Note that the second case is possible only because you use double quotes when creating the string, which allows variables to be interpreted within the string, this makes it less advisable at the performance level to use double quotes when creating chains. makes the PHP engine need to analyze the entire string to verify if it should interpret some value

If it's something additional, please explain your question a bit more.

    
answered by 27.10.2016 / 23:02
source
0

I do not understand very well what is your problem but the query should be without the quotes in the names of the columns:

$sql = "INSERT INTO table(id, name, userID) VALUES (NULL, "Jorge", "NULL o $userID");

If you want to change your query based on the value of $ userID, it would look like this:

$userID=NULL;
if(isset($_POST[$userID])){
    $userID=$_POST['userID'];
}

$sql = "INSERT INTO table(id, name, userID) VALUES (NULL, 'Jorge', $userID)";
    
answered by 27.10.2016 в 22:41