Related
I'm developing a web platform. I'm using PHP and MySQL. I want to insert data to db. My code below.
<?php
session_start();
require_once('../../system/database.php');
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$ownerid = (int)$_SESSION['id'];
$record_time = date('H:i:s');
$record_date = date('Y-m-d');
// form_data
$name = strip_tags($_POST['name']);
$surname = strip_tags($_POST['surname']);
$phone1 = strip_tags($_POST['phone1']);
$birthday = strip_tags(trim($_POST['birthday']));
$gender = strip_tags(trim($_POST['gender']));
$company = strip_tags(trim($_POST['company']));
$address1 = strip_tags(trim($_POST['address1']));
$address2 = strip_tags(trim($_POST['address2']));
$phone2 = strip_tags(trim($_POST['phone2']));
$mail1 = strip_tags(trim($_POST['mail1']));
$mail2 = strip_tags(trim($_POST['mail2']));
$about = strip_tags(trim($_POST['about']));
$type_of = strip_tags(trim($_POST['type_of']));
$visible = strip_tags(trim($_POST['visible']));
$query = "INSERT INTO contact (ownerid, name, surname, birthday, gender, address1, address2, phone1, phone2, mail1, mail2, about, type_of, visible, time, date) VALUES('$ownerid', '$name', '$surname', '$birthday', '$gender', '$address1', '$address2', '$phone1', '$phone2', '$mail1', '$mail2', '$mail1', '$mail2', '$about', '$type_of', '$visible', '$record_time', '$record_date')";
$result = mysqli_query($connection, $query);
mysqli_error($connection);
} else {
header('Location: ../new_contact.php');
}
But my MySQL code does not work and write any error message!
wrong number of column in values clause (you repeat two time mail1 and mail2 ) try
$query = "INSERT INTO contact
(ownerid, name, surname, birthday, gender, address1, address2, phone1,
phone2, mail1, mail2, about, type_of, visible, time, date)
VALUES('$ownerid', '$name', '$surname', '$birthday', '$gender', '$address1', '$address2', '$phone1',
'$phone2', '$mail1', '$mail2', '$about', '$type_of', '$visible', '$record_time', '$record_date')";
Edit insert variables to be like this
$query = "INSERT INTO contact (ownerid, name, surname, birthday, gender, address1, address2, phone1, phone2, mail1, mail2, about, type_of, visible, time, date) VALUES(".$ownerid.", '".$name."', '".$surname."', '".$birthday."', '".$gender."', '".$address1."', '".$address2."', '".$phone1."', '".$phone2."', '".$mail1."', '".$mail2."', '".$about."', '".$type_of."', '".$visible."', '".$record_time."', '".$record_date."')";
You need to concate PHP variable properly and also need correct query format. Try this
$query = "INSERT INTO `contact` (ownerid, name, surname, birthday, gender, address1, address2, phone1, phone2, mail1, mail2, about, type_of, visible, time, date) VALUES('".$ownerid."','".$name."', '".$surname."', '".$birthday."','".$gender."', '".$address1."', '".$address2."', '".$phone1."','".$phone2."', '".$mail1."','".$mail2."', '".$about."', '".$type_of."', '".$visible."', '".$record_time."', '".$record_date."')";
If you have access to PHPMyAdmin, place the query in the SQL section and test it. Sometimes it can give you clues as where to look for issues.
As ScaisEdge says, you have 16 entries as keys, but 18 entries as values into your insert. It can be helpful to use coding software when using PHP that highlights like words (notepad++ is free and does this) Eclipse is another. Also, place your query into PHPMyAdmin "SQL" and test your query to see if it gives you any results or throws an error. It will point where in the string to start looking for your error.
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)");
}
}
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)";
I am really having a tough time understanding what syntax to use. I am VERY green when it comes to coding. I copied this code from another website that is working just fine but they are hooked up to a 4.3 version of mysql- can anyone help?This is the error-
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 '1, city, state, zip, ) VALUES ('', , ' 1', '', '', '','' )' at line 1
Here is my code:
mysql_select_db("membership70", $con);
$name=mysql_real_escape_string($_POST['Name']);
$address1=mysql_real_escape_string($_POST['Address1']);
$city=mysql_real_escape_string($_POST['city']);
$state=mysql_real_escape_string($_POST['state']);
$zip=mysql_real_escape_string($_POST['zip']);
$email=mysql_real_escape_string($_POST['email']);
$sql="INSERT INTO Members (name, email, adress 1, city, state, zip, ) VALUES ('$name', , '$address 1', '$city', '$state', '$zip','$email' )";
if (!mysql_query($sql,$con)) {
die('Error: ' . mysql_error());
}
You have speces in address [space] 1 both places.
$sql="INSERT INTO Members (name, email, adress 1, city, state, zip, ) VALUES ('$name', , '$address 1', '$city', '$state', '$zip','$email' )";
Also you have an extra colon after zip and the order of the values does not match the order you gave the column names. Try:
$sql="INSERT INTO Members (name, email, adress1, city, state, zip) VALUES ('$name', '$email', '$address1', '$city', '$state', '$zip')";
Get rid of the space in adress 1 and your variable $adress 1....not allowed (spaces that is)
That statement clearly tells you the SQL syntax is broken. Take a closer look into your statement in the $sql variable. echo it to debug it properly.
$sql = "INSERT INTO .....";
echo $sql; die();
At first glace, there shouldn't be a blank neither the column adress nor the corresponding php variable. And you should sort the order of your columns. email is declare second, so don't put the value of it at the end.
So, instead of...
$sql="INSERT INTO Members (name, email, adress 1, city, state, zip, ) VALUES ('$name', , '$address 1', '$city', '$state', '$zip','$email' )";
...try...
$sql="INSERT INTO Members (name, email, adress1, city, state, zip) VALUES ('$name', '$email', '$address1', '$city', '$state', '$zip' )";
You have a column adress 1 there, that's not a valid column name in mysql and it could not have worked on any mysql version. What columns does your table have?
Also, your second value is empty, put some value in there (or a default one). And a comma at the end of the columns list. Email at the wrong place.
Maybe this would work?
$sql = "INSERT INTO Members (name, email, adress1, city, state, zip) ".
"VALUES ('$name', '$email', '$address1', '$city', '$state', '$zip')";
(I'm not sure about the third column, should it spell address1?)