I have a form with data I will like to insert in 2 different tables (order and order_etails). Here is what I did. But it is inserting in only 1 form.
<?php
include '../db/connect.php';
$sql="INSERT INTO order_etails (part_id, quantity, price, status_id,order_id)
VALUES
('$_POST[part_id]','$_POST[quantity]','$_POST[price]','$_POST[status_id]','$_POST[order_id] '),
('$_POST[part_id2]','$_POST[quantity2]','$_POST[price2]','$_POST[status_id2]','$_POST[order _id2]'),
('$_POST[part_id3]','$_POST[quantity3]','$_POST[price3]','$_POST[status_id3]','$_POST[order _id3]')";
$sql1="INSERT INTO order (platform)
VALUE
('$_POST[platform]')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "record(s) added";
mysqli_close($con);
?>
I have also tried this:
<?php
include '../db/connect.php';
$sql="INSERT INTO order_details (part_id, quantity, price, status_id,order_id)
VALUES
('$_POST[part_id]','$_POST[quantity]','$_POST[price]','$_POST[status_id]','$_POST[order_id]'),
('$_POST[part_id2]','$_POST[quantity2]','$_POST[price2]','$_POST[status_id2]','$_POST[order_id2]'),
('$_POST[part_id3]','$_POST[quantity3]','$_POST[price3]','$_POST[status_id3]','$_POST[order_id3]');
INSERT INTO order (platform)
VALUE
('$_POST[platform]')";
mysql_query($sql1);
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "record(s) added";
mysqli_close($con);
?>
Try VALUES instead of VALUE in your second query.
Also, you don't seem to actually execute both queries in either of your examples. You should have something like:
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
if (!mysqli_query($con,$sql1))
{
die('Error: ' . mysqli_error($con));
}
echo "record(s) added";
You should also consider wrapping the two executions in a transaction, so that if the 2nd insert fails the first will also be rolled back.
First of all you need using mysql multi query.
$sql="INSERT INTO order_details (part_id, quantity, price, status_id,order_id)
VALUES
('$_POST[part_id]','$_POST[quantity]','$_POST[price]','$_POST[status_id]','$_POST[order_id]'),
('$_POST[part_id2]','$_POST[quantity2]','$_POST[price2]','$_POST[status_id2]','$_POST[order_id2]'),
('$_POST[part_id3]','$_POST[quantity3]','$_POST[price3]','$_POST[status_id3]','$_POST[order_id3]');
$sql.= INSERT INTO order (platform)
VALUES
('$_POST[platform]')";
if (mysqli_multi_query($link, $sql)) {
//do action
}
Thank you for both of you. It worked and here is how I did it:
<?php
include '../db/connect.php';
$sql="INSERT INTO order_details (part_id, quantity, price, status_id,order_id)
VALUES
('$_POST[part_id]','$_POST[quantity]','$_POST[price]','$_POST[status_id]','$_POST[order_id]'),
('$_POST[part_id2]','$_POST[quantity2]','$_POST[price2]','$_POST[status_id2]','$_POST[order_id2]'),
('$_POST[part_id3]','$_POST[quantity3]','$_POST[price3]','$_POST[status_id3]','$_POST[order_id3]')";
$sql1="INSERT INTO order (platform)
VALUES
('$_POST[platform]')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
if (!mysqli_query($con,$sql1))
{
die('Error: ' . mysqli_error($con));
}
echo "record(s) added";
?>
Related
we want to insert the value in only bank...but not inserting the 0 value in cash...so how to do it
if(!isset($_POST[ $cash]) && ($_POST[ $bank] != ""))
{
$query1="INSERT INTO balance_entry(cust_id,amount, discount, total_amt, date, modified_date) VALUES ('$cust_id','$amount','$discount','$total_amt','$today', '$today')";
if (!mysql_query($query1,$conn))
{
die('Error: ' . mysql_error());
}
$query1="INSERT INTO payment_detail(bill_id, date) VALUES ((select max(bill_id) from balance_entry),'$today')";
if (!mysql_query($query1,$conn))
{
die('Error: ' . mysql_error());
}
$query1="INSERT INTO bank_detail(p_id,bank,date) VALUES ((select max(p_id) from payment_detail),'$bank','$today')";
if (!mysql_query($query1,$conn))
{
die('Error: ' . mysql_error());
}
$query1="INSERT INTO credit_detail(p_id,bill_id,debit_amount,credit_amount,date) VALUES ((select max(p_id) from payment_detail),(select max(bill_id) from balance_entry),'$debit_amount','$credit_amount','$today')";
if (!mysql_query($query1,$conn))
{
die('Error: ' . mysql_error());
}
}
Make sure that the column can be NULL. Then you can just do like this:
INSERT INTO whatever (column1, column2)
VALUES ("whatever", NULL);
column2 will now be NULL
I'm trying to use mysqli_insert_id() to get the last id value from one table and insert it into another table. The following code gets me a value of 0 for the ID (but the timestamp is correct). Any ideas? I think there are a few ways to do this but I can't seem to make it work.
<?php
$con=mysqli_connect("hostname","dbname","password","db");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql = "INSERT INTO `thelineexchange`.`lines` (`id` ,`line` ,`author` ,`email` ,`time`)
VALUES (NULL, '".$_POST['input-poem-text']."', '".$_POST['input-author']."', '".$_POST['input-email']."',NOW( ));";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
$id = mysqli_insert_id($con);
$sqlmsg = "INSERT INTO `linesid` (`id`, `time`) VALUES ('".$id."', NOW())";
mysqli_query($sqlmsg);
;
$url = 'yourline.php';
echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';
?>
My form has an student ID , email and Name ...and checkboxes
I managed to store studentid and checkboxes in a table and created a new table to store all the other info related to the student id
Table 1
studentid ----- checkboxselections....
Table 2
studentid -----email ---- name
I have to insert into queries , one for studentid and checkboxes and one for (Table2)
$sql="INSERT INTO courses (studentid, ckb)
VALUES ('$studentid', '$cc')";
$sql2="INSERT INTO studentinfo (studentid, email, name)
VALUES ('$studentid', '$email', $fname)";
$sql2 fails to store data however $sql stores data just fine , how can I fix this ?
here's the full code
$studentid = mysqli_real_escape_string($dbcon, $_GET['studentid']); //echo $studentid;
$email = $dbcon->real_escape_string($_GET['email']);
$fname = $dbcon->real_escape_string($_GET['fname']);
$name = $_GET['ckb'];
if(isset($_GET['ckb'])) //checkboxes
{
foreach ($name as $courcess){
$cc=$cc. $courcess.',';
}
}
$sql="INSERT INTO courses (studentid, ckb)
VALUES ('$studentid', '$cc')";
$sql2="INSERT INTO studentinfo (studentid, email, name)
VALUES ('$studentid', '$email', $fname)";
if (!mysqli_query($dbcon,$sql)) {
die('Error: ' . mysqli_error($dbcon));
}
echo " Thank you for using IME Virtual Registeration ";
mysqli_close($dbcon);
?>
my form method is GET
i recommended you to use prepare statements and transaction instead mysqli query .
you forgot mysqli_query with second query:
$sql="INSERT INTO courses (studentid, ckb)
VALUES ('{$studentid}', '{$cc}')";
$sql2="INSERT INTO studentinfo (studentid, email, name)
VALUES ('{$studentid}', '{$email}', '{$fname}')";
if (!mysqli_query($dbcon,$sql)) {
die('Error: ' . mysqli_error($dbcon));
}
if (!mysqli_query($dbcon,$sql2)) {
die('Error: ' . mysqli_error($dbcon));
}
echo " Thank you for using IME Virtual Registeration ";
mysqli_close($dbcon);
?>
here is the example with prepare statement, and you don't need to use real_escape_string"
$stmt = $mysqli->prepare("INSERT INTO courses VALUES (?, ?)");
$stmt->bind_param('ds', $studentid, $cc);
$stmt->execute();
$stmt = $mysqli->prepare("INSERT INTO studentinfo VALUES (?, ?, ?)");
$stmt->bind_param('dss', $studentid, $email, $fname);
$stmt->execute();
Please Try this
if (!mysqli_query($dbcon,$sql))
{
die('Error: ' . mysqli_error($dbcon));
}
else
{
if (!mysqli_query($dbcon,$sql2))
{
die('Error: ' . mysqli_error($dbcon));
}
else
{
echo " Thank you for using IME Virtual Registeration ";
}
}
mysqli_close($dbcon);
if (!mysqli_query($dbcon,$sql))
{
die('Error: ' . mysqli_error($dbcon));
}
else
{
if (!mysqli_query($dbcon,$sql2)) {
die('Error: ' . mysqli_error($dbcon));
}
}
Hope it will help.....
PHP code is inserting blank records when inserting data in the database with the _POST Method, However when I use _GET everything works fine.
Thanks in Advace.
<?php
$con=mysqli_connect("localhost","root","*******","student");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO Student (textnames, fathername, mom, occu, homenum, paddress, offcontact, Course, District, State, pincode, emailid, dob, mobileno)
VALUES
('$_POST[textnames]','$_POST[fathername]','$_POST[mom]','$_POST[occu]','$_POST[homenum]','$_POST[paddress]','$_POST[offcontact]','$_POST[Course]','$_POST[District]','$_POST[State]','$_POST[pincode]','$_POST[emailid]','$_POST[dob]','$_POST[mobileno]')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "Successfully Added Record";
mysqli_close($con);
?>
Any suggestions how to avoid this.....
a good rule of thumb is checking PHP Variables availability with phpinfo (this is related to your web server configuration).
Try and add:
echo phpinfo();
exit;
just before your
if (!mysqli_query($con,$sql))
The reason is that you are most probably doing the post not same as get
'$_POST[textnames]'// is wrong
'$_POST["textnames"]'// is correct
and same with all others. You must have used quotes to get these values without quotes your index like textnames would be incorrect both for GET and POST
try to initialise the $_POST to a local variable and use the query as follows
<?php
$a=$_POST['textname'];
$b=$_POST['fathername'];
$c=$_POST['mom'];'
$d=$_POST['occu'];
$e=$_POST['homenum'];
$f=$_POST['paddress'];
$g=$_POST['offcontact'];
$i=$_POST['Course'];
$j=$_POST['District'];
$k=$_POST['State'];
$l=$_POST['pincode'];
$m=$_POST['emailid'];
$o=$_POST['dob'];
$p=$_POST['mobileno']
$con=mysqli_connect("localhost","root","*******","student");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO Student (textnames, fathername, mom, occu, homenum, paddress, offcontact, Course, District, State, pincode, emailid, dob, mobileno) VALUES('$a','$b','$c','$d','$e','$f','$g','$i','$j','$k','$l','$m','$o','$p')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "Successfully Added Record";
mysqli_close($con);
?>
First check whether your form is sending value by
if(isset($_POST["submit"]))
{
Your insert query here
}
also in form try using following code
<form methode = "POST" action = "">
</form>
It works with the _REQUEST i think it'll do
<?php
$con=mysqli_connect("localhost","root","********","student");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO Student (textnames, fathername, mom, occu, homenum, paddress, offcontact, Course, District, State, pincode, emailid, dob, mobileno)
VALUES
('$_REQUEST[textnames]','$_REQUEST[fathername]','$_REQUEST[mom]','$_REQUEST[occu]','$_REQUEST[homenum]','$_REQUEST[paddress]','$_REQUEST[offcontact]','$_REQUEST[Course]','$_REQUEST[District]','$_REQUEST[State]','$_REQUEST[pincode]','$_REQUEST[emailid]','$_REQUEST[dob]','$_REQUEST[mobileno]')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "Successfully Added Record";
mysqli_close($con);
?>
You can change your code like this. The point is you missed the single quotes when accessed the post variables.
<?php
$con=mysqli_connect("localhost","root","*******","student");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO Student (textnames, fathername, mom, occu, homenum, paddress, offcontact, Course, District, State, pincode, emailid, dob, mobileno)
VALUES
('".$_POST['textnames']."','".$_POST['fathername']."','".$_POST['mom']."','".$_POST['occu']."','".$_POST['homenum']."','".$_POST['paddress']."','".$_POST['offcontact']."','".$_POST['Course']."','".$_POST['District']."','".$_POST['State']."','".$_POST['pincode']."','".$_POST['emailid']."','".$_POST['dob']."','".$_POST['mobileno']."')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "Successfully Added Record";
mysqli_close($con);
}
}
?>
I have a the following code for inputing data in a database..i specifically echoed the values to see whether they have correct values or not...they have correct values but the values i get in the database are totally different.
Here is my code
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("sm_sample");
$source=$_POST['source'];
$username=$_POST['username'];
$location=$_POST['location'];
$category=$_POST['category'];
$complaint=$_POST['complaint'];
$status=$_POST['status'];
$date=$_POST['date'];
echo $source.$username.$location.$category.$complaint.$status.$date;
$sql="INSERT INTO sample VALUES(ID=NULL,source='$source',username=
'$username', location='$location', category='$category',complaint=
'$complaint',date='$date',status='$status')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
echo "<BR>";
echo "<a href='usercom1.php'>View result</a>";
mysql_close($con)
?>
the values i get in the database r like this:
List data from mysql
Source Username Location Category Complaint Date Status Update
0 Singapore 0 0000-00-00 Pending Edit
The correct syntax:
$sql="INSERT INTO `sample`(`ID`,`source`,`username`, `location`,`category`,`complaint`,`date`,`status`)
VALUES (0, '$source','$username','$location','$category','$complaint','$date','$status')";
later edit ... you are using wrong mysql_query and connection syntax
$con = mysql_connect("localhost","root","") or die('database connection?');
mysql_select_db("sm_sample", $con) or die('wrong database?');
// and for $_POST you sould use mysql_real_escape_string
$source = mysql_real_escape_string($_POST['source']);
// ........................................
$sql="INSERT INTO `sample`(`ID`,`source`,`username`, `location`,`category`,`complaint`,`date`,`status`)
VALUES (0, '$source','$username','$location','$category','$complaint','$date','$status')";
mysql_query($sql) or die('Error: '.mysql_error().': '.mysql_errno());
// ........................................
mysql_close($con);
<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
echo ('Could not connect: ' . mysql_error());
}
mysql_select_db("sm_sample",$con);
$source=$_POST['source'];
$username=$_POST['username'];
$location=$_POST['location'];
$category=$_POST['category'];
$complaint=$_POST['complaint'];
$status=$_POST['status'];
$date=$_POST['date'];
echo $source.$username.$location.$category.$complaint.$status.$date;
$sql="INSERT INTO sample ('source','username','location','category','complaint','status') VALUES('$source','$username','location','category','complaint','status' )";
if (!mysql_query($sql))
{
echo ('Error: ' . mysql_error());
}
echo "1 record added";
echo "<BR>";
echo "<a href='usercom1.php'>View result</a>";
mysql_close($con);
?>
First thing you do not have to add id if it is auto increment and date if it uses current timestamp and one more thing that never use die(); , use echo instead.
You should provide only VALUES of data with no column names:
$sql="INSERT INTO sample VALUES(ID, '$source', '$username', '$location', '$category', '$complaint', '$date', '$status')";
Also if you have only one DB connection you can not to define $con variable in mysql_query(). Like this: mysql_query($sql).
The problem is with the following line:
<?php
$sql="INSERT INTO sample VALUES(ID=NULL,source='$source',username='$username', location='$location', category='$category',complaint=
'$complaint',date='$date',status='$status')";
?>
If you check the result in the database, you'll see that the values are getting in the wrong order, use this instead:
<?php
$sql="INSERT INTO sample(ID, source, username, location, category, complaint, date, status) VALUES(NULL, '$source', '$username', '$location', '$category', '$complaint','$date','$status')";
?>
PLEASE read what Albireo posted in his comment. Your code is extremely vulnerable.