The following table query is not executing properly, what is the correct syntax?

$m_query = 'Insert into'.$job_id.'(`mobile`,`routeID`,`status`) values ('$mobile','$routeID','Sent')';

Hi, you are enclosing the query into single quotes:

$a = 'apples';
$b = 'oranges';

So in order to append another variable you have to use the dot operator:

$c = 'list '. $b . ' and ' .$a;

Basically is this. You have done that when defining the table name ($job_id), so the query would turn to:

$m_query = 'Insert into '.$job_id.'(`mobile`,`routeID`,`status`) values ('.$mobile.','.$routeID.',"Sent")';

Note:

  • the space after the into keyword
  • also 'Sent' becomes "Sent"

But it will not work, unless $mobile and $routeID are integers, if those are strings, then you have to add quotes (double quotes in this case):

$m_query = 'Insert into '.$job_id.'(`mobile`,`routeID`,`status`) values ("'.$mobile.'","'.$routeID."',"Sent")';

This can be refactored with double quotes:

$m_query = "insert into {$job_id} (`mobile`, `routeID`, `status`) values ('{$mobile}', '{$routeID}', 'Sent')";

In any case, you really don't want to create a query like this, you want to use prepared statements, as explained here:

thanks cereal for your solution, one more similar issue, the code is below.
the rows are not inserting into to table

$values = '("'.$mobile.'","'.$routeID.'","INVALID")';

$m_query = 'Insert into `'. $job_id. '` (`mobile`,`routeID`,`status`) values ' .$values;

Your quotes are wrong, MySQL needs single quotes:

$values = "('$mobile', '$routeID', 'INVALID')";

MySQL needs single quotes:

P. that should not make difference:

or I'm missing something? :)

@blueguy777

if you using mysql_* then try to add mysql_error() after the query to catch the error, for example:

$q = mysql_query($m_query) or die(mysql_error());

Show also what would be the end query.

You're right. Guess I've been stuck with the ansi_quotes setting for too long.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.