php only not null value insertion in mysql - php

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

Related

Get ID from last insert into table

Im inserting into multiple tables , and need to get the ID from the last insert into table1 and use that as a variable in the insert for table 2.
The IDs is auto incremented.
The query's will be run once a submit button has been clicked in a form.
Query's:
$sql = "INSERT INTO table1 (Text) VALUES ($T1text);";
$sql = "INSERT INTO table2 (table1ID,Text) VALUES ($table1id, $T2text);";
table1 {id,Text}
table2 {id,table1.id,Text}
There is a PHP function for that (Decprecated, Removed in 7.0 !)
Or the according mysql/mysqli/pdo functions.
Solutions
PHP (Deprecated)
Example
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('mydb');
mysql_query("INSERT INTO mytable (product) values ('kossu')");
printf("Last inserted record has id %d\n", mysql_insert_id());
Source: http://php.net/manual/en/function.mysql-insert-id.php
Mysql/Mysqli/PDO
Mysqli
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john#example.com')";
if (mysqli_query($conn, $sql)) {
$last_id = mysqli_insert_id($conn);
echo "New record created successfully. Last inserted ID is: " . $last_id;
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
PDO
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john#example.com')";
// use exec() because no results are returned
$conn->exec($sql);
$last_id = $conn->lastInsertId();
Source: http://www.w3schools.com/php/php_mysql_insert_lastid.asp

Using mysqli_insert_id to insert last ID from one table into another

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.'">';
?>

Two tables for one php/MySQL form with a common field?

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.....

Insert data in multiple Mysql tables using single php form

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";
?>

Error when inserting into SQL table with PHP

This is my code:
function function() {
$isbn = $_REQUEST["isbn"];
$price = $_REQUEST["price"];
$cond = $_REQUEST["cond"];
$con = mysql_connect("localhost","my_usernam", "password");
if (!$con) die('Could not connect:' . mysql_error());
mysql_select_db("my_database",$con);
$sql="INSERT INTO 'Books' (isbn, price, condition)
VALUES ('$isbn','$price','$cond')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
mysql_close($con);
return "It works";
But when run it results in:
Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''Books' (isbn, price....
Anyone know why this is happening?
You should use backticks instead of single quotes for table and field names:
$sql="INSERT INTO `Books` (`isbn`, `price`, `condition`)
VALUES ('$isbn','$price','$cond')";
will work.
ps. to prevent all kinds of nasty security holes, escape the input fields with:
$isbn = mysql_real_escape_string($_REQUEST["isbn"]);
// etc etc for all fields
Wrap table names in backticks, not quotes, and make sure to escape your input for security:
$sql="INSERT INTO `Books` (`isbn`, `price`, `condition`)
VALUES ('" . mysql_real_escape_string($isbn) . "',
'" . mysql_real_escape_string($price) . "',
'" . mysql_real_escape_string($cond) . "')";

Categories