Vaibhav Vaibhav - 5 months ago 6
SQL Question

Unable to insert data in a MySQL table using PHP

I am trying to insert data from one

table
to another using
PHP


<?php
$sel=mysqli_query($whmcscon, "SELECT * FROM `tbltickets` WHERE `id` = 92376");
$row=mysqli_fetch_array($sel);
mysqli_query($formcon, "INSERT INTO `ft_form_17`(`case_number`, `status`, `subject`, `client_group`, `client_name`,`source`) VALUES (".$row[tid].",".$row[status].",".$row[title].",\"GroupA\",\"vaibhav batra\",\"Web\")");
?>


The problem is insert query is only working when I only use
".$row[tid]."
but it fails if there are other variables in array of
$row
.

Here is var_dump of my Select query

dbarray(46) {
[0]=> string(5) "92376" ["id"]=> string(5) "92376"
[1]=> string(13) "2016062492376" ["tid"]=> string(13) "2016062492376" [2]=> string(1) "1" ["did"]=> string(1) "1"
[3]=> string(4) "3565" ["userid"]=> string(4) "3565"
[4]=> string(1) "0" ["contactid"]=> string(1) "0"
[5]=> string(33) "Vaibhav Batra" ["name"]=> string(33) "Vaibhav Batra" [6]=> string(22) "some@example.com" ["email"]=> string(22) "some@example.com"
[7]=> string(0) "" ["cc"]=> string(0) ""
[8]=> string(8) "x8MSn3rt" ["c"]=> string(8) "x8MSn3rt"
[9]=> string(19) "2016-06-24 00:20:26" ["date"]=> string(19) "2016-06-24 00:20:26"
[10]=> string(55) "Unable to send emails" ["title"]=> string(55) "Unable to send emails"
[11]=> string(61) "I am not able to send emails" ["message"]=> string(61) "I am not able to send emails"
[12]=> string(9) "Follow Up" ["status"]=> string(9) "Follow Up"
[13]=> string(6) "Medium" ["urgency"]=> string(6) "Medium"
[14]=> string(12) "Nitesh Gupta" ["admin"]=> string(12) "Nitesh Gupta" [15]=> string(0) "" ["attachment"]=> string(0) ""
[16]=> string(19) "2016-06-24 08:47:34" ["lastreply"]=> string(19) "2016-06-24 08:47:34"
[17]=> string(1) "0" ["flag"]=> string(1) "0"
[18]=> string(1) "1" ["clientunread"]=> string(1) "1"
[19]=> string(7) "69,76,1" ["adminunread"]=> string(7) "69,76,1"
[20]=> string(1) "0" ["replyingadmin"]=> string(1) "0"
[21]=> string(19) "0000-00-00 00:00:00" ["replyingtime"]=> string(19) "0000-00-00 00:00:00"
[22]=> string(0) "" ["service"]=> string(0) ""
}

Answer

You're missing many single-quotes, both in the array references and inside the query itself.

Change your query to this:

mysqli_query($formcon,  "INSERT INTO `ft_form_17`(`case_number`, `status`, `subject`, `client_group`, `client_name`,`source`) VALUES ('".$row['tid']."','".$row['status']."','".$row['title']."','GroupA','vaibhav batra','Web')");