I'm pretty new to PHP code.
I have a tabel that I want to insert in to but for some reason the insert statement does not work. The tabel has an ScanID column that AUTO_INCREMENT 's. I've tried a few things like just leaving the ScanID out of the statement but that didn't work either ( I also tried swapping the NULL with ' '). I was able to insert in to other tables where there isn't an ID that AUTO_INCREMENT's so my pretty sure that my connection works.
<?php
$xml=simplexml_load_file("someFile.xml");
$con =new PDO("mysql:host=localhost;dbname=testDB",'root','');
$ScanType="someType";
$start_date=$xml ->start_datetime;
$end_date=$xml ->finish_datetime;
$TargetTargetID="1";
$stmt=$con->prepare('insert into
Scan(ScanID, ScanType, start_date, end_date, TargetTargetID) values
(:ScanID, :ScanType, :start_date, :end_date, :TargetTargetID)');
$stmt->bindValue('ScanID',NULL);
$stmt->bindValue('ScanType',$ScanType);
$stmt->bindValue('start_date',$start_date);
$stmt->bindValue('end_date',$end_date);
$stmt->bindValue('TargetTargetID',$TargetTargetID);
$stmt->execute();
$ScanID=$con->lastInsertId();
echo $ScanID;
?>
EDIT: this worked for me
$stmt=$con->prepare('insert into
Scan(ScanType, start_date, end_date, TargetTargetID) values
(:ScanType, :start_date, :end_date, :TargetTargetID)');
$stmt->bindValue('ScanType',$ScanType);
$stmt->bindValue('start_date',$start_date);
$stmt->bindValue('end_date',$end_date);
$stmt->bindValue('TargetTargetID',$TargetTargetID);
$stmt->execute();
No need to pass auto increment column value in insert query just remove from columns and values to
$stmt=$con->prepare('insert into
Scan(ScanType, start_date, end_date, TargetTargetID) values
(:ScanType, :start_date, :end_date, :TargetTargetID)');
Remove from bind values
$stmt->bindValue('ScanID',NULL);
Related
I have two tables
transaction_tbl
order_tbl
I inserted data in transaction_tbl with a primary key autoincrement transaction_no.
Now, I need to insert data in order_tbl with columns
orderitem_no(AI, PK),
transaction_no,
product_sku,
quantity
How can I sync the transaction_no from transaction table in one process?
I just assign data to variables then:
My current code is
$sql0 = "INSERT INTO transaction_tbl (transaction_no, transaction_date, customer_name) VALUE ('', now(), '$customer_name');";
$x=0;
while ($x!=5){
$sql1 = "INSERT INTO order_tbl
(orderitem_no, transaction_no, product_sku, quantity)
VALUE ('', '', '$product_sku', '$quantity');";
$x=$x+1;
}
Nothing next. Sorry, Im super lost.
Im planning to run ths sql's in one process.
transaction_no in transaction_tbl is auto increment.
I have fk_trans_no in order_tbl.
And then I totally lost. orderitem_no is auto increment but what will I do to the transaction_no?
I am trying to insert a record to a table with 2 column but I get this error.
My error starts in part of the execute. Anyone that can help me out with this ?
I am using PDO.
My code:
global $conn_kl;
$sql = $conn_kl->prepare("INSERT INTO order_producten VALUES (?,?)");
$sql->execute(array($product_id, $bewerking_id));
The issue is here:
INSERT INTO order_producten VALUES (?,?)
here columns are not defined in this query, in this case it is expected that you have to pass the values for all columns in the table. But you want to insert the values for only 2 columns, so please please specify that columns names like:
INSERT INTO order_producten(column_name1, column_name2) VALUES (?,?)
order_producten will have more or less than two columns and you are setting only two values.
Please specify columns after table name. for example,
INSERT INTO order_producten(id, name) VALUES(?, ?)
For example, code something like this were working for me:
global $conn_kl;
$sql = $conn_kl->prepare("INSERT INTO `order_bewerkingen` VALUES (null, ?, ?, ?)");
$sql->execute(array($order_id, $method, $position));
I'm pretty new in PHP and MYSQL. I've got this form that I use to enter data into a database I created. I add up values from two fields in separate columns and insert the value into a third column (TOTAL_IN). Then I subtract another value (VALUE3) that's in another field from the value in a field in the third column (TOTAL_IN) and put that value in a different column. All these are in the same table. It works just fine, but the problem is that when I open up my database I see that the data has been inserted 20 or 100 times! How can I stop the data from being inserted so many times?
Please note that the submit button was clicked only once.
Here is what I use:
$sql="INSERT INTO $tbl_name (id, date, value1, value2, total_in, value3, value4)
SELECT '','$date','$value1','$value2',('$value1'+'$value2') AS SUM,
'$value3',(('$value1'+'$value2')-$value3) AS SUM
FROM $tbl_name";
$result=mysql_query($sql);
any help?
Thanks in advance!
EDIT::: Here is what my code looks like now after your suggestions:
//From Ruddy's post
$total_in=$value1+$value2;
$value4=($value1+$value2)-$value3;
//From Amit's post
$sql="INSERT INTO $tbl_name(id, date, value1, value2, total_in, value3, value4) VALUES ('', '.$date.', '.$value1.', '.$value2.', '.$total_in.', '.$value3.', '.$value4.')";
$result=mysqli_query($sql);
it works but it still enters the data many times.
$sql="INSERT INTO $tbl_name (id, date, value1, value2, total_in, value3, value4)
VALUES( '', '".$date."', ".$value1.", ".$value2.", ".$value1+$value2.",
".$value3.",".$value1+$value2-$value4.")";
$result=mysql_query($sql);
You need to concatenate the variables in php while writing in the query please see the syntax.
see here
What you were doing was select all the rows of the table and inserting into all.
$sum= $value1+$value2;
$sum2= ($value1+$value2)-$value4;
$sql="INSERT INTO $tbl_name (id, date, value1, value2, total_in, value3, value4) VALUES ('', '$date', '$value1', '$value2','$sum','$value3','$sum2')"
$result=mysql_query($sql);
I hate sums in the statement, so I took them out.
As it is, your query will INSERT as many rows as returned from the SELECT statement! Looks like your SELECT returns as many rows as there are in $tbl_name and you are re-inserting them in the same table!!
I have been working on my first webapp and I hit a bit of a wall. I have a table in my db set up as follows:
student_id(student_id, first_name, last_name, bdate, etc...)
I also have several tables for classes set up similarly to this
class_table(id, student_id, quiz_1, quiz_2, etc....)
student_id is how I would like to track everything, from my understanding, this would be a primary key that would become a foreign key on the class tables.
What I would like to do is create an entry for the student on each class table when the php script I am writing creates a new student. This is what my query looks like:
$query = "INSERT INTO student_id(0, '$first_name', '$last_name'.... etc);".
"INSERT INTO class_table(0, LAST_INSERT_ID(), '$quiz_1', $quiz_2'...etc)";
Is this the right way to do this? I keep getting an error from my mysqli_query... so I am guessing this is where the problem is. How would I achieve this?
mysqli_query() (and mysql_query()) will only execute a single query. You would need to perform two calls to mysqli_query() or use mysqli_multi_query(), which will execute multiple queries in one call.
You're missing the VALUES clause:
$query = "INSERT INTO student_id VALUES (0, '$first_name', '$last_name'.... etc);".
"INSERT INTO class_table VALUES (0, LAST_INSERT_ID(), '$quiz_1', '$quiz_2'...etc)";
and you will need to use the mysqli_multi_query() function. See the example at http://www.php.net/manual/en/mysqli.multi-query.php#106126:
if ($mysqli->multi_query($query)) {
$i = 0;
do {
$i++;
} while ($mysqli->next_result());
}
if ($mysqli->errno) {
echo "Batch execution prematurely ended on statement $i.\n";
var_dump($statements[$i], $mysqli->error);
}
You could also create a stored procedure, and call it with all the needed parameters:
CALL insert_student('$first_name', '$last_name', '$quiz_1', $quiz_2', ... etc);
Here's an example:
CREATE PROCEDURE add_student(
IN v_first_name VARCHAR(50),
IN v_last_name VARCHAR(50),
IN v_quiz_1 VARCHAR(255),
IN v_quiz_2 VARCHAR(255)
)
DETERMINISTIC
MODIFIES SQL DATA
proc: BEGIN
START TRANSACTION;
INSERT INTO student_id VALUES (0, v_first_name, v_last_name);
IF ROW_COUNT() <= 0 THEN
ROLLBACK;
SELECT 0 AS result;
LEAVE proc;
END IF;
INSERT INTO class_table VALUES (0, LAST_INSERT_ID(), v_quiz_1, v_quiz_2);
COMMIT;
SELECT 1 AS result;
END;
I'm pulling data from a calendar feed and each event in the calendar has a unique $EventID string. I'm using PHP.
I have a SQL database with an Event_ID column. These IDs are strings. I need to be able to compare my $EventID against the Event_ID column and put in in the database if it's not there.
I have a primary key set up to auto increment in the database, and I was thinking I can set up a loop to increment through those and compare each to the $EventID, but I'm wondering if there is a better way-maybe a PHP function I don't know about?
I've got a whole lot of code, but basically I've got:
<?php
$EventID = $event->id; //This is the event ID
mysql_query("INSERT INTO myTable
(Event_ID, Date_added, Date_edited)
VALUES
('$EventID', '$dateAdded', '$lastEdited')");
?>
So how do I set up a conditional to check all the Event_IDs that are already in the database against the $EventID?
$query = "SELECT * FROM `myTable` WHERE `Event_ID`='$EventID' ";
$result = mysql_query($query);
if (!mysql_num_rows($result))
// INSERT QUERY
Check if the Event ID is present, If not insert it
You could just skip the "Select" query and do an "INSERT IGNORE" instead:
mysql_query("INSERT IGNORE INTO myTable
(Event_ID, Date_added, Date_edited)
VALUES
('$EventID', '$dateAdded', '$lastEdited')");
this will leave existing Event_id's, and just add new records if required.