Conversion from MySQL to mysqli not working - php

I am in a process of converting my Existing MySQL to mysqli
But I can't get this piece of code correct
mysql_query("INSERT INTO `sendmsg`(`sendname`, `recievename`, `subject`, `body` , `mdate`, `mtime`) VALUES ('$sendname','$recievename','$subject','$body','$msgdate','$msgtime')");
$new_id = mysql_insert_id();
mysql_query("INSERT INTO `recievemsg`(`msgid`, `sendname`, `recievename`, `subject`, `body`, `mdate`, `mtime`, `status`) VALUES ($new_id,'$sendname','$recievename','$subject','$body','$msgdate','$msgtime','UNREAD')");
What I tried is given below but didn't work
$mysqli->query("INSERT INTO `sendmsg`(`sendname`, `recievename`, `subject`, `body` , `mdate`, `mtime`) VALUES ('$sendname','$recievename','$subject','$body','$msgdate','$msgtime')");
$new_id = mysqli_insert_id();
$mysqli->query("INSERT INTO `recievemsg`(`msgid`, `sendname`, `recievename`, `subject`, `body`, `mdate`, `mtime`, `status`) VALUES ($new_id,'$sendname','$recievename','$subject','$body','$msgdate','$msgtime','UNREAD')");
The Problem is with the $new_id = mysqli_insert_id(); statement bcoz the first query is executing

For mysqli Object oriented style to get the last inserted id use this
$mysqli->insert_id ;
http://www.php.net/manual/en/mysqli.insert-id.php
So your queries will be as
$mysqli->query("INSERT INTO `sendmsg`(`sendname`, `recievename`, `subject`, `body` , `mdate`, `mtime`) VALUES ('$sendname','$recievename','$subject','$body','$msgdate','$msgtime')");
$new_id = $mysqli->insert_id;
$mysqli->query("INSERT INTO `recievemsg`(`msgid`, `sendname`, `recievename`, `subject`, `body`, `mdate`, `mtime`, `status`) VALUES ($new_id,'$sendname','$recievename','$subject','$body','$msgdate','$msgtime','UNREAD')");

Related

How to enter POST data into database?

Sample code:
echo $mylinks;
Outputted echo:
http://www.google1.com|http://www.google2.com
I want to insert each of these into a database table.
Here's what I was trying to use:
INSERT INTO `backlinks` (`id`, `link`, `apikey`, `date`) VALUES (NULL, '".$mylinks."', '11bebe13ae7fe257d9ddba22a9d1eea3', CURRENT_TIMESTAMP);
The problem is, it's inserting into the database like this basically:
INSERT INTO `backlinks` (`id`, `link`, `apikey`, `date`) VALUES (NULL, 'http://www.google1.com|http://www.google2.com', '11bebe13ae7fe257d9ddba22a9d1eea3', CURRENT_TIMESTAMP);
Whereas I want each URL to be entered into its own row like this:
INSERT INTO `backlinks` (`id`, `link`, `apikey`, `date`) VALUES (NULL, 'http://www.google1.com', '11bebe13ae7fe257d9ddba22a9d1eea3', CURRENT_TIMESTAMP);
INSERT INTO `backlinks` (`id`, `link`, `apikey`, `date`) VALUES (NULL, 'http://www.google2.com', '11bebe13ae7fe257d9ddba22a9d1eea3', CURRENT_TIMESTAMP);
URLs are separated by a pipe (|) - there's normally more than one URL that's submitted, but sometimes there's only a single URL - in case that makes a difference.
So how do I submit the URLs into my database so each is on a separate row?
mysql will not split that for you, you need to split it yourself.
$stmt = $pdo->prepare("INSERT INTO `backlinks` (`id`, `link`, `apikey`, `date`) VALUES (NULL,:link, '11bebe13ae7fe257d9ddba22a9d1eea3', CURRENT_TIMESTAMP)");
foreach (explode("|", $mylinks) as $link) {
$stmt->execute(array(":link" => $link))
}
You can convert this string to an array with php explode function and after that insert data like this:
$array = explode('|', $mylinks);
foreach($array as $key => $value)
//Insert $value into table

How do you pass multiple sql statements using php [duplicate]

This question already has answers here:
php/mysql with multiple queries
(3 answers)
Closed 7 years ago.
This is what i want to do but with lots of records. When i try this i get this kind of error:the error message i get
this is my currant php and when i submit it no record is added.
$sql = "INSERT INTO `people` (`id`, `firstname`, `lastname`, `email`, `reg_date`) VALUES (NULL, 'firstnameA', 'surnameA', 'example1#email.com', CURRENT_TIMESTAMP);
INSERT INTO `people` (`id`, `firstname`, `lastname`, `email`, `reg_date`) VALUES (NULL, 'firstnameB', 'surnameB', 'example#email.com', CURRENT_TIMESTAMP);
" ;
However this code works but i am only adding one record
$sql = "INSERT INTO `people` (`id`, `firstname`, `lastname`, `email`, `reg_date`) VALUES (NULL, 'tom', 'walker', 'tom#walker.com', CURRENT_TIMESTAMP);
"
You just need to duplicate the values portion, like this:
$sql = "INSERT INTO `people` (`id`, `firstname`, `lastname`, `email`, `reg_date`)
VALUES (NULL, 'tom', 'walker', 'tom#walker.com', CURRENT_TIMESTAMP),
(NULL, 'bob', 'jones', 'bob#jones', CURRENT_TIMESTAMP)";
Use mysqli_multi_query check out:
http://php.net/manual/en/mysqli.multi-query.php

Couldn't insert into Database: Column count doesn't match value count at row 1

I have a table in hte same database similar to this that has no difference in syntax or format. Yet I get the above mentioned error? I have done everything I know to fix it, but not sure what I have done wrong. I have an image of the structure as well.
$sql = "INSERT INTO `FormInfo` (`first_name`,
`last_name`,
`company`,
`address`,
`province`,
`postal`,
`telephone`,
`fax`,
`email`,
`comment`)
VALUES ('$good_data[first_name]',
'$good_data[last_name]',
'$good_data[company]',
'$good_data[address]',
'$good_data[province]',
'$good_data[postal]',
'$good_data[telephone]',
'$good_data[email]',
'$good_data[comment]')";
mysqli_query($cxn, $sql) or die ("Couldn't insert into Database: " . mysqli_error($cxn));
What am I missing here?
You are missing your $good_data[fax] argument
EDIT: Also, please consider the way you are calling your objects in your array is bad practice. See this for more information
try to change,
$sql = "INSERT INTO `FormInfo` (`first_name`, `last_name`, `company`, `address`, `province`, `postal`, `telephone`, `fax`, `email` ,`comment`) VALUES ('$good_data[first_name]', '$good_data[last_name]', '$good_data[company]', '$good_data[address]', '$good_data[province]', '$good_data[postal]', '$good_data[telephone]', '$good_data[email]', '$good_data[comment]')";
to
$sql = "INSERT INTO `FormInfo` (`first_name`, `last_name`, `company`, `address`, `province`, `postal`, `telephone`, `fax`, `email` ,`comment`) VALUES ('$good_data[first_name]', '$good_data[last_name]', '$good_data[company]', '$good_data[address]', '$good_data[province]', '$good_data[postal]', '$good_data[telephone]', '$good_data[email]', '$good_data[comment]', '$good_data[fax]')";
You are missing insert value for fax column:
$SQL = "INSERT INTO `FormInfo`
(`first_name`,
`last_name`,
`company`,
`address`,
`province`,
`postal`,
`telephone`,
`fax`,
`email`,
`comment`
)
VALUES (
'$good_data[first_name]',
'$good_data[last_name]',
'$good_data[company]',
'$good_data[address]',
'$good_data[province]',
'$good_data[postal]',
'$good_data[telephone]',
'$good_data[fax]',
'$good_data[email]',
'$good_data[comment]'
)";

how can I add same value in two different tables

i m trying to add custom list of tags using excel file and I have done most of the work. but i am confused that how can I add same value in 2 different tables columns.
i had inserted the values in
INSERT INTO `steve`.`wp_terms` (`term_id`, `name`, `slug`, `term_group`) VALUES (NULL, 'tag99', 'tag99', '0');
and
INSERT INTO `steve`.`wp_term_taxonomy` (`term_taxonomy_id`, `term_id`, `taxonomy`, `description`, `parent`, `count`) VALUES (NULL, '21', 'post_tag', '', '0', '0');
now i want to add term_id in both table .. could somebody tell me how can i do that?
AND term_id is database generated value.
INSERT INTO `steve`.`wp_term_taxonomy` (`term_taxonomy_id`, `term_id`, `taxonomy`, `description`, `parent`, `count`) VALUES (NULL, LAST_INSERT_ID, 'post_tag', '', '0', '0');
If I follow correctly your first query inserts into a table and creates a new id for that record, then you want to use that ID in the next query to insert into a different table right? If that's the case the above query should work for your second one using LAST_INSERT_ID for the term_id value.
To update the question with your new code it should, I emphasize should, work with this:
$sql2 = "INSERT INTO " . $table2 . " (`term_taxonomy_id`, `term_id`, `taxonomy`, `description`, `parent`, `count`) VALUES (NULL, LAST_INSERT_ID, 'post_tag', '', '0', '0')";
$wpdb->query($sql);
$wpdb->query($sql2);
The way you have it above you are overwriting your first query before you execute it.
Also I don't understand how you're inserting nulls into an auto-increment field, that alone should be throwing an error. Honestly both your queries should be leaving their base ids (the auto increment ones) out of the query entirely like this:
$sql2 = "INSERT INTO " . $table2 . " (term_id, taxonomy, description, parent, count) VALUES (LAST_INSERT_ID, 'post_tag', '', '0', '0')";
Use mysql_insert_id()
mysql_query("INSERT INTO `steve`.`wp_terms` (`term_id`, `name`, `slug`, `term_group`) VALUES (NULL, 'tag99', 'tag99', '0')");
$lastInsertId = mysql_insert_id();
mysql_query("INSERT INTO `steve`.`wp_term_taxonomy` (`term_taxonomy_id`, `term_id`, `taxonomy`, `description`, `parent`, `count`) VALUES (NULL, '" . $lastInsertId . "', 'post_tag', '', '0', '0')");
NOTE: To use mysql_insert_id(), term_id in wp_terms must be set to auto increment.
you need to use ALTER table and add that column's definition/constraints into the other table, here's the manual

PDO 2300 upon insert though no rows exist

$compost = $dbh->prepare("INSERT INTO `table`
(`PRIMARY`, `some`, `content`, `rows`, `and`, `boolean`)
VALUES ('', :binded, :param, :inputs, :blah, 0)") ;
the primary key iterates automatically and clasicaly this means my inserts do not need to give actual input for that column just ' ' is fine.
However this keeps throwing a 2300 error.
no rows are indexes or keys just the primary and a dumped all the table rows before attempting this script.
So I removed the primary
$compost = $dbh->prepare("INSERT INTO `table`
( `some`, `content`, `rows`, `and`, `boolean`)
VALUES ( :binded, :param, :inputs, :blah, 0)") ;
but it still throws the error, going to make a new table and try that
Leave the primary key if it is auto increment.
$compost = $dbh->prepare("INSERT INTO `table`
(`some`, `content`, `rows`, `and`, `boolean`)
VALUES (:binded, :param, :inputs, :blah, 0)") ;
If your PRIMARY column name is 'PRIMARY' you can try:
$compost = $dbh->prepare("INSERT INTO `table`
(`PRIMARY`, `some`, `content`, `rows`, `and`, `boolean`)
VALUES (PRIMARY, :binded, :param, :inputs, :blah, 0)") ;
or just not to mention that field at all:
$compost = $dbh->prepare("INSERT INTO `table`
(`some`, `content`, `rows`, `and`, `boolean`)
VALUES (:binded, :param, :inputs, :blah, 0)") ;

Categories