Renaming table name - php

I am trying to rename a table's name from a specific database. I have tried with both of the query given below, but it shows the same error message. I can't understand my mistakes.
The query
1st one :
<?php
$id = $_POST['id'];
$department = $_POST['department'];
$dept_id = $_POST['dept_id'];
$olddept_id = $_SESSION['olddept_id'];
if(isset($_POST['submit']))
{
$order = "UPDATE department SET department='$_POST[department]', dept_id='$_POST[dept_id]' WHERE id='$_POST[id]'";
mysql_query($order) or die (mysql_error());
mysql_query("RENAME TABLE $olddept_id TO $dept_id;") or die (mysql_error());
}
and
2nd one :
<?php
$id = $_POST['id'];
$department = $_POST['department'];
$dept_id = $_POST['dept_id'];
$olddept_id = $_SESSION['olddept_id'];
if(isset($_POST['submit']))
{
$order = "UPDATE department SET department='$_POST[department]', dept_id='$_POST[dept_id]' WHERE id='$_POST[id]'";
mysql_query($order) or die (mysql_error());
mysql_query("ALTER TABLE $olddept_id RENAME $dept_id;") or die (mysql_error());
}
The error message is :
"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 'TO CSEE' at line 1"
The table names which i want to edit, are also stored in a table, named "department". This is done successfully, but the table doesn't renaming.
-thank you

Show the exact SQL that's generated by those queries. I'm guessing you're using a reserved word for the original table name, which means you'd have to escape it with backticks:
RENAME reservedword TO CSEE

Related

insert data from a table and insert into another table in different database

I have 2 database that link together. I need to retrieve data from that table and insert those column into a table in different database based on their Unique id number.
<?php
$handle = mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_query("USE shop",$handle);
$query = "SELECT ModelCode,Class FROM shopfloor_pro WHERE CommNo = '0985560712'";
$result = mysql_query($query);
while ($data = mysql_fetch_object($result)){
$variable1 = $data->ModelCode;
$variable2 = $data->Class;
mysql_query("USE vt",$handle);
$sql = "INSERT INTO track SET
t_model_code = '$variable1',
t_class = '$variable2' WHERE t_comm_no = '0985560712'";
if (!mysql_query($sql)) {
echo '<p>Error adding data into database: ' . mysql_error() . '</p>';
}
mysql_query("USE paintshop",$handle);
}
?>
this is the data that i want to retrieve
this is where i want to put the data
When i run the code it shows
"Error adding data into database: 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 t_comm_no = '0985560712'' at line 3"
You can most likely do this in a single query - but as pointed out the mysql api has been deprecated a long time ago and totally removed from PHP 7+.
To do the query in a single operation you might try like this:
insert into `vt`.`track` (`t_model_code`,`t_class` )
select `ModelCode`,`Class` from `shop`.`shopfloor_pro` where `CommNo`='0985560712'

SQL query to retrieve columns names list

So I've been struggling with my database to get it to give me the name of columns contained within a table.
Here's my PHP :
$sql = "SELECT * FROM hacklvrf_db.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = N'generators'" ;
$result = mysqli_query($con, $sql);
$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
$generators = $row['generators'];
foreach ($row as $lol) {
echo ($lol);
}
For some reason this isn't answering with anything (PHP doesn't pop an error but my variables seem to be empty) and I don't really understand what I'm missing.
echo (gettype ($row));
Shows a 'NULL'
I know this question has been asked before and I actually got my SQL query from other places but I since I can't work it out... here I am !
Thanks in advance guys !
Change
$sql = "SELECT * FROM hacklvrf_db.INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = N'generators'";
To
$sql = "SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA`='database-name' AND `TABLE_NAME`='table-name'";
Used your sql query after changing database name and table name, I got error
1064 - 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 '.COLUMNS WHERE TABLE_NAME
Updated Code (Just put your database name and table name in query)
<?php
$sql = "SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA`='database-name' AND `TABLE_NAME`='table-name'";
$result = mysqli_query($con, $sql);
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
echo $row['COLUMN_NAME']."<br>";
}
?>
For more info, click MySQL query to get column names?

Syntax error near 'WHERE emp_id = $emp_id' at line 1

I've been trying to make a php form page for the users of my website.
When I open the .php page I got the standard error message :
Could not enter data: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'WHERE emp_id = $emp_id' at line 1
Can anybody help me with the syntax of these commands ???
The Code is here :
<?php
include 'dbc.php';
$emp_id = $_POST['emp_id'];
$emp_name = $_POST['emp_name'];
$emp_address = $_POST['emp_address'];
$emp_salary = $_POST['emp_salary'];
$emp_date = $_POST['join_date'];
$sql = 'INSERT INTO employee SET emp_salary = $emp_salary WHERE emp_id = $emp_id';
mysql_select_db($dbname);
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not enter data: ' . mysql_error());
}
echo "Entered data successfully\n";
mysql_close($conn);
?>
The query syntax is wrong. You have to use UPDATE query. As you are enclosing the query in single quote, the PHP variables won't get replaced. So change
$sql = 'UPDATE employee SET emp_salary = $emp_salary WHERE emp_id = $emp_id';
to
$sql = "UPDATE employee SET emp_salary = $emp_salary WHERE emp_id = $emp_id";
or
$sql = 'UPDATE employee SET emp_salary = '.$emp_salary.' WHERE emp_id = '.$emp_id;
Hi again all you good people
I thanks very much for the amount of answers !
The right solution was found and the problem is solved with this statement:
$sql = "UPDATE `employee` SET `emp_salary` = '$emp_salary' WHERE emp_id = '$emp_id'";
Most of you was inded right about the syntax and the choice about UPDATE.
The above statement function very well, but it was a bit hard to find the way.
Thanks again for all your kindness, help and time to answer my help
John Engelsby-Hansen
$sql = 'UPDATE employee SET emp_salary=$emp_salary WHERE emp_id = '.$emp_id;
Insert query should be
$sql = 'INSERT INTO employee SET emp_salary = $emp_salary'; // it is valid without where clause
and there is no meaning for Where clause in Insert Qqery
Actually, if You want to update record then write an update query where we have to set values for column
Like
$sql = 'Update employee SET emp_salary= $emp_salary WHERE emp_id = $emp_id';
Your Update Query is wrong
If its an UPDATE query then it should be
UPDATE employee SET emp_salary = $emp_salary WHERE emp_id = $emp_id
And if you are trying to insert a row then how can you use a WHERE condition?
WHERE condition are used in cases of UPDATE QUERY, NOT INSERT QUery

MySQL UPDATE gives error

I have tried for hours now to update a MySQL table with PHP.
I used the following code (and several others) but it gives an error message:
$id = $_GET['id'];
if(isset($_POST['descr'])){
$go = $_POST['descr'];
mysql_query("UPDATE Rooms SET Desc='$go' WHERE Room_ID='$id'")
or die(mysql_error());
}
mysql_close($conn);
with the 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 'Desc='This room is the primary test-room. It is?' WHERE Room_ID='11'' at line 1"
The form is called: "descr", the table "Rooms", the field that needs update is "Desc" and it should be where the corresponding ID is, based on a dynamic URL.
If I write echo = $go it outputs the correct data, so I suppose it's the php.
It DOES connect correctly to the database.
Desc is a special word in mysql
try it by escape
mysql_query("UPDATE Rooms SET `Desc`='$go' WHERE Room_ID='$id'")
Assuming that ID is a number:
$id = $_GET['id'];
if(isset($_POST['descr'])){
$go = $_POST['descr'];
mysql_query("UPDATE Rooms SET `Desc`='".$go."' WHERE Room_ID=".$id.")
or die(mysql_error());
}
mysql_close($conn);
Desc is reserved for ORDER BY! Enclose it with '`' symbols!
mysql_query("UPDATE `Rooms` SET `Desc` = '".$go."' WHERE `Room_ID` = ".$id.")
or die(mysql_error());

MYSQL Insert syntax Error

I have two codes to put data into database but it is generating error, check out the code below.
$email = "example#hotmail.com"; //email
$pass = "helloworld"; //password
$fname = "Example"; //first name
$lname = "Man"; //last name
$birth = "2012-2-1"; //birthday
$gender = "male"; //gender
$site_prefix = "my_"; //table prefix
THIS CODE DOESNT WORK AND OUTPUT AN ERROR
$sql = "
INSERT INTO `{$site_prefix}login` (`email`,`pass`)
VALUES ('$email','$pass');
INSERT INTO `{$site_prefix}users` (`fname`,`lname`,`birthday`,`gender`)
VALUES ('$fname','$lname','$birth','$gender')";
mysql_query($sql,$con) or die(mysql_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 'INSERT INTO my_users (fname,lname,birthday,gender) VALUES ('Example','Ma' at line 2
THIS CODE WORK NORMALLY
$sql = "INSERT INTO `{$site_prefix}login` (`email`,`pass`) VALUES ('$email','$pass');";
$sql1 = "INSERT INTO `{$site_prefix}users` (`fname`,`lname`,`birthday`,`gender`) VALUES ('$fname','$lname','$birth','$gender')";
mysql_query($sql,$con) or die(mysql_error());
mysql_query($sql1,$con) or die(mysql_error());
mysql_query cannot process multiple statements in one query.
From the docs:
mysql_query() sends a unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified link_identifier
Use mysqli (with mysqli_multi_query) if you need this functionality.

Categories