Cannot rename MySQL table - php

Please help me to discover syntax error in my rename_table script. What i want is add date to the table name, but something goes wrong.
Now here's the code:
$date = date('d-m-Y');
$query = "RENAME order TO order".$date;
if(mysql_query($ren)){
...

You have to use backticks for order as it is a reserved keyword. Also you are executing the query wrongly.
if(mysql_query($ren))
^
Replace $ren with $query as your query is stored in a variable $query, not $ren..
So try with
$query = "RENAME TABLE `order` TO order".$date;
if(mysql_query($query))

change
$query = "RENAME order TO order".$date;
to
$query = "RENAME `order` TO `order".$date."`";

You cant use - sign as table name, use _ or dmy format 06nov2014

Try this
$date = date('d-m-Y');
$query = "RENAME `order` TO `order".$date."`";
if(mysql_query($ren))

Related

How to include a PHP variable inside MySQL query

I wanna add an extra column to my database and give it the name price_23-05-2019.
How do I put this into a working query?
I have this right now, which is clearly not working:
$date = date("d-m-Y");
$query =
"ALTER TABLE `products_05_2019`
ADD price_'.$date.' DECIMAL(7,2)";
($result = mysqli_query($link, $query))
You really shouldn't have separate columns for each date. There should just be a date column, with the date as the value, and a separate row for each date.
But if you have to do it this way, here's how to solve it.
If you use - in a column name, you have to enclose the name in backticks.
$date = date("d-m-Y");
$query =
"ALTER TABLE `products_05_2019`
ADD `price_$date` DECIMAL(7,2)";
$result = mysqli_query($link, $query);
But it would probably be better to use _ instead of -.
$date = date("d_m_Y");
$query =
"ALTER TABLE `products_05_2019`
ADD price_'.$date.' DECIMAL(7,2)";
$result = mysqli_query($link, $query);
As #ficuscr says above, you may want to have a look at the design of your database so you don't have to create columns from code.
Anyway, what I use to do when I have a column name depending on code is create a new variable and then include it into the query:
$date = date("d-m-Y");
$column_name = 'price_'.$date;
$query = "ALTER TABLE `products_05_2019` ADD `$column_name` DECIMAL(7,2)";

Can't update Date field in MySql using PHP

I have a DATE field in my table, and I'm trying to update it using the following code:
$query = mysqli_query($conn,$sql);
$todaydate = date("Y-m-d");
$sqlDate = date('Y-m-d', strtotime($todaydate));
$sql="UPDATE Library SET Loaned=1, LoanedDate=$sqlDate WHERE BookId=$bookId";
$query = mysqli_query($conn,$sql);
It updates the "Loaned" field fine, but always sets the Date field to "0000-00-00". Can anyone indicate what I'm doing wrong?
Youre missing ' quotes in your update query
Just use something like this
$sql="UPDATE Library SET Loaned=1, LoanedDate='$sqlDate' WHERE BookId='$bookId'";
$query = mysqli_query($conn,$sql);
You must have to add Quotes to the values... other wise the date may seem like an invalid integer to mysql. Here's how:
$query = mysqli_query($conn,$sql);
$todaydate = date("Y-m-d", time()); //<== DON'T FORGET THE 2ND ARGUMENT TO date(): TIME-STAMP. YOU MAY USE: time()
$sqlDate = date('Y-m-d', strtotime($todaydate));
$sql ="UPDATE Library SET Loaned=1, LoanedDate='{$sqlDate}' WHERE BookId='{$bookId}'";
$query = mysqli_query($conn,$sql);
Why don't you use only MySQL function to update date
Use this
$sql = "UPDATE Library SET Loaned = 1, LoanedDate = DATE(NOW())
WHERE BookId = '".$bookId."'";
Change your query with this code
$sql="UPDATE Library SET Loaned=1, LoanedDate= current_date() WHERE BookId='".$bookId."'";
Also check data structure in database.
This query is working fine:
$current_date = strtotime(date('Y-m-d H:i:s'));
$sql = "UPDATE user SET dt_added = '".$current_date."' WHERE id = '$id' ";
You're not concatenating the Date string properly in the SQL query.
Use ' and . operator to concatenate the string. Like this,
$sql="UPDATE Library SET Loaned=1, LoanedDate='".$sqlDate."' WHERE BookId='".$bookId."'";
Altough it's good practice to use Prepared statement to pass arguments in SQL query.
Learn more about PHP Prepared Statements
Looking closer, I would just do:
$sql="UPDATE Library SET Loaned=1, LoanedDate='".$todaydate."' WHERE BookId='".$bookId."'";
instead of
$sql="UPDATE Library SET Loaned=1, LoanedDate='".$sqlDate."' WHERE BookId=$bookId";

MySQL select with variable not working

$select = "SELECT name FROM table_name WHERE location ='".$loc."' ";
$findname = mysql_query($select) or die(mysql_error());
I keep getting this error! I have tried everything!!!
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 'WHERE location ='Florida'' at line 1
$loc is determined by the following:
<input type="text" name="loc"> in the HTML
$loc = $_POST['loc']; in the PHP
Try this , use mysql_escape_string or mysql_real_escape_string mysql safe string functions
$select = "SELECT `name` FROM `table_name` WHERE `location` ='".mysql_escape_string($loc)."' ";
$findname = mysql_query($select) or die(mysql_error());
$select = "SELECT name FROM table_name WHERE location ='".mysql_real_escape_string($loc)."' ";
$findname = mysql_query($select) or die(mysql_error());
Just check for any single quotes or double quotes in the location variable. That might be a problem.
Use str_replace(find,replace,string) to replace single and double quotes in the string.
Example, when you can contain a double quote in the $loc variable.
$select = "SELECT name FROM table_name WHERE location ='Flo"rida' ";
The query will end at Flo.
use mysql_real_escape_string after WHERE location =
I fixed it. I was using a variable for the table name and was referencing a null value. Thanks for the help!

MySQL query mishandling date field

I've got a PHP/MySQL script that is yielding strange results on a date field. All along the process, my dates are fine until the very end. The final result has every entry in the date field as '0000-00-00'. I'm totally stuck and don't know what else to do. I can tell that this is an issue with PHP not interpreting this as a date, but I don't know how to fix it. Here is my code:
$sql = "CREATE TABLE temp_workouts (my_date date, sg_id int(11), loc_id int(11))";
$result = mysql_query($sql);
if (!$result) {
$tag_success = "failure";
$tag_message = mysql_error();
echo encodeJSON($tag_success, $tag_message);
die();
}
$sql = "SELECT * FROM my_table";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$my_date = $row['my_date'];
echo $my_date . " "; //<--this output looks perfect
$sql = "INSERT INTO temp_table (my_date) VALUES ($my_date)";
$result2 = mysql_query($sql);
}
die();
When I flip over to MyPHPAdmin and look at the table, the entire column my_date contains '0000-00-00'. How can I get PHP to recognize this as a 'Y-m-d' formatted date? Thanks. I appreciate any help.
I suspect the issue is that you haven't enclosed a string literal in single quotes:
INSERT INTO temp_table (my_date) VALUES ('$my_date')
^--- ^--- string literals in single quotes
Otherwise, the statement is probably something like:
... VALUES (2013-08-22)
MySQL isn't converting that into a valid date, issuing a warning message, and inserting a "zero" date.
Your immediate problem is that you don't use quotes around date values in your insert statement.
Change
$sql = "INSERT INTO temp_table (my_date) VALUES ($my_date)";
to
$sql = "INSERT INTO temp_table (my_date) VALUES ('$my_date')";
^ ^
Now, you can just use INSERT ... SELECT syntax to achieve your goal in one go
INSERT INTO temp_table (my_date)
SELECT my_date
FROM my_table
Therefore this part of your code
$sql = "SELECT * FROM my_table";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$my_date = $row['my_date'];
echo $my_date . " "; //<--this output looks perfect
$sql = "INSERT INTO temp_table (my_date) VALUES ($my_date)";
$result2 = mysql_query($sql);
}
can be changed to
$sql = "INSERT INTO temp_table (my_date)
SELECT my_date FROM my_table";
$result2 = mysql_query($sql);
On a side note: Consider switching to either PDO or MySQLi and use prepared statements.
Try this one...This will re-convert it to date, and then save..
$dt = strtotime($row['my_date']);
$date = date("Y-m-d",$dt);
$sql = "INSERT INTO temp_table (my_date) VALUES ({$date})";

Unknown column in where clause

This script is supposed to retrieve the CustomerID for the Customer_First_Name and Customer_Last_Name that has been entered into a form.
$query = "SELECT CustomerID FROM customer WHERE Customer_First_Name = `.$db_customer_first_name.` AND Customer_Last_Name = `.$db_customer_last_name.`";
$result = mysql_query($query)
or die(mysql_error());
echo $result;
echo $query;
when the script runs I get this error:
Unknown column '.Christopher.' in 'where clause'
the query is never printed on the screen.
any help is appreciated.
Your quotes are bad use ' instead of the tick `
You have to use single quotes for strings. Try again:
$query = "SELECT CustomerID FROM customer WHERE Customer_First_Name = '".$db_customer_first_name."' AND Customer_Last_Name = '".$db_customer_last_name."'";
This should work:
$query = "SELECT CustomerID FROM customer WHERE Customer_First_Name = '$db_customer_first_name' AND Customer_Last_Name = '$db_customer_last_name'";
You need to use normal single quotes for values. Also you don't need to break the string if you're using double quotes - variables are detected within the string.
Make sure you're also correctly escaping your strings for mysql to prevent injection.
Better still, look at moving to mysqli and using prepared statements.
Use ' instead of remove `
$query = "SELECT CustomerID FROM customer WHERE Customer_First_Name = '".$db_customer_first_name."' AND Customer_Last_Name = '".$db_customer_last_name."'";

Categories