My Code.
<?php //data.php
require_once 'db.php';
// Get values from form
$Fname = $_POST['first_name'];
$Lname = $_POST['last_name'];
$web = $_POST['web'];
$email = $_POST['email'];
$date = $_POST['date'];
// Insert data into mysql
$sql="INSERT INTO users (first_name, last_name, web, email, date)
VALUES ('$Fname', '$Lname', '$web', '$email', '$date', NOW())";
$result = mysql_query($sql);
$sql="SELECT DATE_ADD('$date', INTERVAL 30 day)";
$result = mysql_query($sql);
// if successfully insert data into database, displays message "Successful".
if($result){
header('Location: ../index.php');
}
else {
echo "ERROR";
}
// close mysql
mysql_close();
?>
My problem, i want to add those 30 days to the date column in database when form execute`s this file and inserts fname in fname, web in web, and date in date + 30 interval.
Thank you.
$sql="INSERT INTO users (first_name, last_name, web, email, date)
VALUES ('$Fname', '$Lname', '$web', '$email', '$date', NOW())";
$result = mysql_query($sql);
***$sql="SELECT DATE_ADD('$date', INTERVAL 30 day)";
$result = mysql_query($sql);***
Use the following query :
$sql="INSERT INTO users (first_name, last_name, web, email, date)
VALUES ('$Fname', '$Lname', '$web', '$email',DATE_ADD('$date', INTERVAL 30 day) , NOW())";
Do like this...
$sql="INSERT INTO `users` (`first_name`, `last_name`, `web`, `email`, `date`)
VALUES ('$Fname', '$Lname', '$web', '$email', DATE_ADD('$date', INTERVAL 30 DAY))";
Try this:
$sql="INSERT INTO users (first_name, last_name, web, email, date)
VALUES ('$Fname', '$Lname', '$web', '$email', '$date', DATE_ADD(NOW(), INTERVAL 30 DAY))";
$result = mysql_query($sql);
>> you are adding date after query executes, but do the same when query executes.
Thanks
Here is the part you need to change
$sql="INSERT INTO users (first_name, last_name, web, email, date)
VALUES ('$Fname', '$Lname', '$web', '$email', '$date', NOW() + INTERVAL 30 DAY)";
Related
I have a registration page that works perfectly fine when it the first user registers. When the second user registers the INSERT query outputs an error. Here is my code:
if ($err_found != true)
$saveData = "INSERT INTO anc_user_info (CardNumber, UserName, Password, Name,
Surname, IDNumber, Province, Region, Branch, Gender, Language,
PhysicalAddress, Profession, Category, TelNoW, TelNoH, Email,
Cell, ProfilePic, TransactionRef, DepositAmount, DepositerName,
Declaration, UserID, Status, CreateDate)
VALUES( '$CardNumber', '$UserName', md5('$Password'), '$Name', '$Surname',
'$IDNumber', '$Province', '$Region', '$Branch', '$Gender',
'$Language', '$PhysicalAddress', '$Profession', '$Category',
'$TelNoW', '$TelNoH', '$Email', '$Cell', '$ProfilePic',
'$TransactionRef', '$DepositAmount', '$DepositerName',
'$Declaration', '$UserID', 0, NOW())" ;
My Code.
<?php //data.php
require_once 'db.php';
// Get values from form
$Fname = $_POST['first_name'];
$Lname = $_POST['last_name'];
$web = $_POST['web'];
$email = $_POST['email'];
$date = $_POST['date'];
// Insert data into mysql
$sql="INSERT INTO users (first_name, last_name, web, email, date)
VALUES ('$Fname', '$Lname', '$web', '$email', '$date', NOW())";
$result = mysql_query($sql);
$sql="SELECT DATE_ADD('$date', INTERVAL 30 day)";
$result = mysql_query($sql);
// if successfully insert data into database, displays message "Successful".
if($result){
header('Location: ../index.php');
}
else {
echo "ERROR";
}
// close mysql
mysql_close();
?>
My problem, i want to add those 30 days to the date column in database when form execute`s this file and inserts fname in fname, web in web, and date in date + 30 interval.
Thank you.
$sql="INSERT INTO users (first_name, last_name, web, email, date)
VALUES ('$Fname', '$Lname', '$web', '$email', '$date', NOW())";
$result = mysql_query($sql);
***$sql="SELECT DATE_ADD('$date', INTERVAL 30 day)";
$result = mysql_query($sql);***
Use the following query :
$sql="INSERT INTO users (first_name, last_name, web, email, date)
VALUES ('$Fname', '$Lname', '$web', '$email',DATE_ADD('$date', INTERVAL 30 day) , NOW())";
Do like this...
$sql="INSERT INTO `users` (`first_name`, `last_name`, `web`, `email`, `date`)
VALUES ('$Fname', '$Lname', '$web', '$email', DATE_ADD('$date', INTERVAL 30 DAY))";
Try this:
$sql="INSERT INTO users (first_name, last_name, web, email, date)
VALUES ('$Fname', '$Lname', '$web', '$email', '$date', DATE_ADD(NOW(), INTERVAL 30 DAY))";
$result = mysql_query($sql);
>> you are adding date after query executes, but do the same when query executes.
Thanks
Here is the part you need to change
$sql="INSERT INTO users (first_name, last_name, web, email, date)
VALUES ('$Fname', '$Lname', '$web', '$email', '$date', NOW() + INTERVAL 30 DAY)";
So I have two different Order forms. If one is completed and then they check out it generates only one order...
But if they select to add more products it adds another one to the form..
For the first order number I want it to be a random generated number..
But for the SECOND order number at the bottom of the code. I need it to be the same number that was generated for the first order.
How Can I do that?
if($row == 1) {
$sqll = mysqli_query($con, "UPDATE BubbleGum SET pendingOrders=pendingOrders + 1 WHERE Name='".$name."'");
$sqli = mysqli_query($conn, "INSERT INTO Pending (BrainID, name, Type, amount, equipment, orderNumber) VALUES ('$brain', '$name', '$type', '$amount', '$equipment', 'THIS NUMBER NEEDS TO BE RANDOM')");
} else {
$sql = mysqli_query($con, "INSERT INTO Bubblegum (BrainID, name, street, city, state, zip, height, weight) VALUES ('', '$name', '$street', '$city', '$state', '$zip', '$height', '$weight')");
$sqli = mysqli_query($conn, "INSERT INTO Pending (BrainID, name, Type, amount, equipment, orderNumber) VALUES ('$brain', '$name', '$type', '$amount', '$equipment', '')");
}
if(isset($_SESSION['more'])) {
$sqli = mysqli_query($conn, "INSERT INTO Pending (BrainID, name, Type, amount, equipment, orderNumber) VALUES ('$brain', '$name', '$type2', '$amount2', '$equipment2', 'THIS NUMBER NEEDS TO BE THE SAME RANDOM NUMBER AS ABOVE')");
}
}
update:
If I do what has been suggested. Then it only adds one entry into my mysql database... It does NOT add the entry at the bottom.
$random = rand (1 , 10);
while($roww = mysqli_fetch_array($query)) {
//count rows, If exist, then username exist
$row = mysqli_num_rows($query);
$brain = $roww['BrainID'];
if($row == 1) {
$sqll = mysqli_query($con, "UPDATE BubbleGum SET pendingOrders=pendingOrders + 1 WHERE Name='".$name."'");
$sqli = mysqli_query($conn, "INSERT INTO Pending (BrainID, name, Type, amount, equipment, orderNumber) VALUES ('$brain', '$name', '$type', '$amount', '$equipment', '$random')");
} else {
$sql = mysqli_query($con, "INSERT INTO BubbleGum (BrainID, name, street, city, state, zip, height, weight) VALUES ('', '$name', '$street', '$city', '$state', '$zip', '$height', '$weight')");
$sqli = mysqli_query($conn, "INSERT INTO Pending (BrainID, name, Type, amount, equipment, orderNumber) VALUES ('$brain', '$name', '$type', '$amount', '$equipment', '$random')");
}
if(isset($_SESSION['more'])) {
$sqli = mysqli_query($conn, "INSERT INTO Pending (BrainID, name, Type, amount, equipment, orderNumber) VALUES ('$brain', '$name', '$type2', '$amount2', '$equipment2', '$random')");
}
}
As mentioned in the comments, just generate the number outside the scope of the if statements, eg:
$random = rand (1 , 10);
if($row == 1) {
$sqll = mysqli_query($con, "UPDATE BubbleGum SET pendingOrders=pendingOrders + 1 WHERE Name='".$name."'");
$sqli = mysqli_query($conn, "INSERT INTO Pending (BrainID, name, Type, amount, equipment, orderNumber) VALUES ('$brain', '$name', '$type', '$amount', '$equipment', $random)");
} else {
$sql = mysqli_query($con, "INSERT INTO Bubblegum (BrainID, name, street, city, state, zip, height, weight) VALUES ('', '$name', '$street', '$city', '$state', '$zip', '$height', '$weight')");
$sqli = mysqli_query($conn, "INSERT INTO Pending (BrainID, name, Type, amount, equipment, orderNumber) VALUES ('$brain', '$name', '$type', '$amount', '$equipment', '')");
}
if(isset($_SESSION['more'])) {
$sqli = mysqli_query($conn, "INSERT INTO Pending (BrainID, name, Type, amount, equipment, orderNumber) VALUES ('$brain', '$name', '$type2', '$amount2', '$equipment2', $random)");
}
}
This php insert query is not working in MYSQL xampp. I couldn't find any error
QUERY:
$query = "INSERT INTO member (id, username,fname,lname,email, password, salt )
VALUES ( '$username', '$password', '$email', '$salt' )";
you are missing $fname, $lname in query also use NULL for id if auto incremented
$query = "INSERT INTO member (id, username,fname,lname,email, password, salt )
VALUES (NULL, '$username', '$fname', '$lname', '$password', '$email', '$salt' )";
you are passing wrong number of column names.your correct query should look like this:
$query = "INSERT INTO member (username,password,email,salt )
VALUES ( '$username', '$password', '$email', '$salt' )";
You are not inserting values to all the columns specified in the query.
In your query
$query = "INSERT INTO member (id, username,fname,lname,email, password, salt )
VALUES ( '$username', '$password', '$email', '$salt' )";
You are specifying 7 columns and only 4 values .So either add more values or remove unnecessary columns specified in the query like
$query = "INSERT INTO member (username, password,email, salt )
VALUES ( '$username', '$password', '$email', '$salt' )";
OR
$query = "INSERT INTO member (id, username,fname,lname,email, password, salt )
VALUES ('$id', '$username','$fname','$lname','$email', '$password', '$salt' )";
Below is the code that I've tried but I can't seem to make the right tweaks to get the script to run properly.
$sql = 'INSERT INTO clients (first_name, last_name, email) VALUES('.$_POST['first_name'].', '.$_POST['last_name'].', '.$_POST['email'].')
INSERT INTO client_data (client_id, phone, zip, note) VALUES(LAST_INSERT_ID(), '.$_POST['phone'].', '.$_POST['zip'].', '.$_POST['message'].')';
mysql_query($sql) or die (mysql_error());
Any help is much appreciated.
You cannot execute two sql statements with mysql_query();
Use something like this:
$sql = 'INSERT INTO clients (first_name, last_name, email) VALUES('.$_POST['first_name'].', '.$_POST['last_name'].', '.$_POST['email'].')';
mysql_query($sql);
$clientId = mysql_insert_id();
$sql = 'INSERT INTO client_data (client_id, phone, zip, note) VALUES('.$clientId.', '.$_POST['phone'].', '.$_POST['zip'].', '.$_POST['message'].')';
mysql_query($sql) or die (mysql_error());
But please read up on SQL injections and how to prevent them.
Just make 2 queries:
$sql_insert_clients = "INSERT INTO clients (first_name, last_name, email) VALUES(".$_POST['first_name'].", ".$_POST['last_name'].", ".$_POST['email'].")";
mysql_query($sql_insert_clients) or die (mysql_error());
$sql_insert_client_data = "INSERT INTO client_data (client_id, phone, zip, note) VALUES(".mysql_insert_id().", ".$_POST['phone'].", ".$_POST['zip'].", ".$_POST['message'].")";
mysql_query($sql_insert_client_data) or die (mysql_error());
You should break it up into two separate mysql_query calls and use the mysql_insert_id function:
$firstName = mysql_real_escape_string($_POST["first_name"]);
$lastName = mysql_real_escape_string($_POST["last_name"]);
$email = mysql_real_escape_string($_POST["email"]);
$phone = mysql_real_escape_string($_POST["phone"]);
$zip = mysql_real_escape_string($_POST["zip"]);
$message = mysql_real_escape_string($_POST["message"]);
mysql_query("INSERT INTO clients (first_name, last_name, email) VALUES ('{$firstName}', '{$lastName}', '{$email}')") or die(mysql_error());
mysql_query("INSERT INTO client_data (client_id, phone, zip, note) VALUES ('". mysql_insert_id() ."', '{$phone}', '{$zip}', '{$message}')") or die(mysql_error());
Your just missing the semi-colon to split the Insert's
$sql = 'INSERT INTO clients (first_name, last_name, email)
VALUES('.$_POST['first_name'].', '.$_POST['last_name'].', '.$_POST['email'].')'."; ".' INSERT INTO client_data (client_id, phone, zip, note) VALUES(LAST_INSERT_ID(), '.$_POST['phone'].', '.$_POST['zip'].', '.$_POST['message'].')';
mysqli_multi_query($sql) or die (mysql_error());
the SQL query it should be running (with dummy content) is
INSERT INTO clients (first_name, last_name, email) VALUES('test', 'surname', email); INSERT INTO client_data (client_id, phone, zip, note) VALUES(LAST_INSERT_ID(), 'phone', 'zip', 'message');