I have a date field with CURRENT_TIMESTAMP attribute. and I am trying to insert date in the database but the value storing as '0000-00-00 00:00:00'.
My code:
<?php
if (isset($_POST['submit'])) {
$amount = $_POST['amount'];
$sql = "insert into `od_request` (`username`, `amount`, `status`, `date`) values ('$login_session', '$amount', 'Request', 'NOW()')";
$retval = mysql_query($sql);
if($retval) {
$success= "OD Request Sucessfully Sent!";
} else {
$error = "Sorry! Make sure you have entered all the Fields Correctly.";
}
}
?>
Use this request :
INSERT INTO `od_request` (`username`, `amount`, `status`, `date`) VALUES('$login_session', '$amount', 'Request', NOW())
You don't need to put ' around NOW() because is an SQL function
Syntax to insert date is wrong:
use NOW() not 'NOW()'
$sql = "insert into `od_request` (`username`, `amount`, `status`, `date`) values ('$login_session', '$amount', 'Request', NOW())";
date("Y-m-d H:i:s") is the MySQL datetime format
eg.
$sql = "insert into `od_request` (`username`, `amount`, `status`, `date`) values ('$login_session', '$amount', 'Request', '".date("Y-m-d H:i:s")."')"
what about this?
$sql = "insert into 'od_request' ('username', 'amount', 'status', 'date') values ('$login_session', '$amount', 'Request', NOW())"
Related
include "include/connection.php";
$date= date("Y-m-d H:i:s");
$profile_comment="INSERT INTO `infotown_test`.`profile_comments` (`id`, `user_id`, `name`, `email`, `comments`, `date_and_time`) VALUES (NULL, '".$_REQUEST['user_id']."', '".$_REQUEST['name']."', '".$_REQUEST['email']."', '".$_REQUEST['comment']."', '".$date."')";
mysql_query($profile_comment);
header("location:company-profile.php?id=2");(why it's not working?)
Make the "location" Capital to "Location",
header("Location:company-profile.php?id=2");
I know questions like this exist all over, but I can't see what's wrong with my code. The DB works and I run an update query just fine earlier on in the code.
Query 1:
mysql_query("INSERT INTO `login_history` (`memberid`, `ip`, `host`, `location`, `status`, `date`) VALUES ('".$login."', '".$ip."', '".$details->hostname."', '".$loc."', 'success', NOW()");
Query 2:
mysql_query("INSERT INTO `login_history` (`memberid`, `ip`, `host`, `location`, `status`, `date`) VALUES ('".$login."', '".$ip."', '".$details->hostname."', '".$loc."', 'failure', NOW()");
Here is an echo of the string as requested:
INSERT INTO `login_history` (`memberid`, `ip`, `host`, `location`, `status`, `date`) VALUES ('AAL', '**.60.**.**', 'c-174-**-**-**.hsd1.**.comcast.net', 'Town, US', 'success', NOW()
Looks like you are missing the final closing ). Also use prepared statements. Much cleaner and safer. Here is a quick example of what a Prepared Statement would look like (adapted from here) (you wold also need to make other changes to your PHP script to start using them)
$stmt = $mysqli->prepare("INSERT INTO `login_history` (`memberid`, `ip`, `host`, `location`, `status`, `date`) VALUES (?, ?, ?, ?, ?, NOW())")
$stmt->bind_param('sssss', $login, $ip, $details->hostname, $loc, 'failure');
$stmt->execute()
public function multiQueryInsert($query){
if($this->conn->multi_query($query)){
do{
$this->conn->store_result();
/*if($result = $this->conn->store_result()){
while($row = $result->fetch_row()){
return $row;
}*/
//$result->free();
//}
$this->conn->more_results();
}
while($this->conn->next_result());
return true;
}
else{
return $this->conn->errno;
}
$this->conn->close();
}
$query = "INSERT INTO `table_name`(`name`, `phone`, `address`, `email`, `cell`, `pcf`, `church`, `group`, `zone`, `dob`, `occupation`, `status`) VALUES ('$names','$phone','$address','$email','$cell','$pcf','$church','$group','$zone','$dob','$occupation','$status')";
$username = explode(' ',$names);
$fname = strtolower($username[0]);
$password = $data;
$query .= "INSERT INTO `table_name2` (`uid`, `pswd`, `Name`, `Email`) VALUES ('$fname','$password','$names','$email')";
if($db->multiQueryInsert($query) === TRUE){
echo '<div class="success">Partner added successfully</div>';
}
else{
die('Error adding partner: '.$db->conn->error);
}
The first code is the method that execute the multi_query while the other codes are the query passed to the method. The error thrown is Error adding partner:
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:
"INSERT INTO `cec_users` (`uid`, `pswd`, `Name`, `Email`) VALUES ('kjvhm,bhjkl','')" at line 1
Try to use semicolon at the end of first insert query like this:
$query = "INSERT INTO `table_name`(`name`, `phone`, `address`, `email`, `cell`, `pcf`, `church`, `group`, `zone`, `dob`, `occupation`, `status`) VALUES ('$names','$phone','$address','$email','$cell','$pcf','$church','$group','$zone','$dob','$occupation','$status');";
I am having an issue with a MySQL query as follows:
My script generates this as an example query:
INSERT INTO `contacts`(`name`, `phone`, `email`, `city`, `state`, `date`) VALUES ('Test2', '123-456-7890', 'test#test.com', 'mesa', 'az', '04-14-2013')
Which if I drop directly into PHPMyA, works fine. However, the PHP script I am trying to use to send the query from my website is not working and I can't get it figured out. Here it is:
$sql = "INSERT INTO `contacts`(`name`, `phone`, `email`, `city`, `state`, `date`) VALUES ('$name', '$phone', '$email', '$city', '$state', '$date')";
mysql_query($sql);
$result = mysql_query($sql);
if($result)
{
echo("<br>Data Input OK");
}
else
{
echo("<br>Data Input Failed");
}
Nothing makes it to the MySQL DB and no PHP errors are displayed, however, if I echo $sql I get the exact query I posted previously.
Just remove the single line mysql_query($sql); on your code and you will be fine.. But you should better start practicing PHP MySQLi which stands for PHP MySQL Improved, such:
$con = mysqli_connect($host, $user, $password, $password);
$sql = "INSERT INTO `contacts`(`name`, `phone`, `email`, `city`, `state`, `date`) VALUES ('$name', '$phone', '$email', '$city', '$state', '$date')";
$result = mysqli_query($con, $sql);
if($result) {
echo("<br>Data Input OK");
} else {
echo("<br>Data Input Failed");
}
$sql = 'INSERT INTO Table_name (`id`, `name`) VALUES ("1", "php");
You are executing $sql twice in your script wich is causing the error, please remove
mysql_query($sql);
And it will be ready to go
I would also suggest to stop using mysql_query please switch to mysqli or PDO
Are you sure there is a valid connection (..mysql_connect())? Try using the full syntax like so..
$conn = mysql_connect(...);
$result = mysql_query($query, $conn);
Also try forcing a commit after you execute the statement -
$mysql_query("COMMIT", $conn);
You are running mysql_query twice. Reason of the error. Try running the following code.
$sql = "INSERT INTO `contacts`(`name`, `phone`, `email`, `city`, `state`, `date`) VALUES ('$name', '$phone', '$email', '$city', '$state', '$date')";
$result = mysql_query($sql) or die(mysql_error());
if($result){
echo("<br>Data Input OK");
} else{
echo("<br>Data Input Failed");
}
use this
"INSERT INTO `contacts`(`name`, `phone`, `email`, `city`, `state`, `date`) VALUES ('$_POST[name]', '$_POST[phone]', '$_POST[email]', '$_POST[city]', '$_POST[state]', '$_POST[date]')";
Try to use mysql_query($sql,$con); instead of mysql_query($sql);.
if(isset($_POST['submit']))
{
$name=$_POST['name'];
$age=$_POST['age'];
$address=$_POST['address'];
$ins="insert into table_name(`name`,`age`,`address`)values('".$name."','".$age."','".$address."')";
mysql_query($ins);
echo 'data inserted successfully';
}
i have a db query in php that is not inserting into database. Have used this format lots of times but for some reason its not working now. any ideas please
$query = "INSERT INTO `databasename`.`member_users` (`id`, `first_name`, `last_name`, `username`, `password`, `address1`, `address2`, `postcode`, `access`, `expires`) VALUES (NULL, '$fname', '$lname', '$email', '', '$add1', '$add2', '$postcode', '0', '')";
$result = mysql_query($query);
if($result){
echo"query inserted";
}else{
echo "nope";
}
Instead of echo "nope"; I suggest something like :
echo 'error while inserting : ['.mysql_errno().'] '.mysql_error();
echo 'query : '.$query;
This way you will be able to see the exact error and the query that was executed.
It can be a lot of things :
Constraint error with a foreign key
Data type error
Non-existent field
Wrong database or table name
Instead of...
$query = "INSERT INTO `databasename`.`member_users` ..."
do
$query = "INSERT INTO member_users ..."
Hope it works. :)
If databasename and member_users are variables then,
Instead of
$query = "INSERT INTO databasename.member_users...
do
$query = "INSERT INTO $databasename.$member_users...