inserting values using get method into database - php

i am relatively new in php.The problem that i am facing while i inserting values into 'leave' table of my database. the error is given below..
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 lid = 4' at line 1
and here is my code
<?php
include_once 'config.php';
$accept = "accepted";
mysql_query("insert into `leave` (`action`) values ('$accept') where lid = ".$_GET['id'] , $dbCon ) or die(mysql_error());
header('location: admin_leave.php');
?>

You need to use update for this not insert
mysql_query("update leave set `action` ='$accept' where lid = ".$_GET['id'] , $dbCon ) or die(mysql_error());
and don't use mysql_* they are depreciated link: http://php.net/manual/en/function.mysql-db-query.php . Use either PDO/mysqli

INSERT can't use with WHERE, may be you are looking for UPDATE
mysql_query("UPDATE `leave` SET `action`='$accept' where lid = ".$_GET['id'] , $dbCon ) or die(mysql_error());
Your queries are full open for SQL Injection. Start using Mysqli OR PDO with prepared statement.

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'

WHERE id_member = 1 doesn't work in MYSQL

Well I have that code:
$query="INSERT INTO ".$db_prefix."members (badges) VALUES ('$id_badge') WHERE id_member = '$user_id'";
And PHP drop me that 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 'WHERE id_member = '1'' at line 1
What can I do? :/
SORRY FOR EVERY PERSON WHO HAS REPLY TO ME I WAS WORNG WITH THE $QUERY, I HAVE EDITED TO THE CORRECT QUERY, NOW YOU CAN ANSWER ME. THANKS. :D
To every person that have voted me down, I'm starting in mysql... ¬¬
Try it like
$query="UPDATE ".$db_prefix."members SET badges = '$id_badge' WHERE id_member = '$user_id'";
You need to UPDATE the table not INSERT.And Try to avoid mysql_* statements due to the entire ext/mysql PHP extension, which provides all functions named with the prefix mysql_*, is officially deprecated as of PHP v5.5.0 and will be removed in the future.
There are two other MySQL extensions that you can better Use: MySQLi and PDO_MySQL, either of which can be used instead of ext/mysql.
You can't use WHERE in INSERT INTO. Use UPDATE command:
$query = "UPDATE " . $db_prefix . "members SET badges = '" . $id_badge . "' WHERE id_member = '" . $user_id . "'";
You are doing wrong do not insert data just update it
#mysql_query("UPDATE ".$db_prefix."members SET badges = '".$id_badge."' WHERE member_id='".$user_id."'");
$query = "UPDATE '".$db_prefix."members'
SET badges=$id_badge
WHERE id_member =".$user_id;
try putting up below line:
$query = mysql_query("SELECT `badges` FROM ".$db_prefix." members WHERE `id_member` = ".(int)$id_del_usuario."");
your second query:
$query="INSERT INTO ".$db_prefix."members SET (badges) VALUES (".$id_badge.") WHERE `id_member` = ".$user_id."";

getting error check ur mysql syntax near 'order(`pcode`)

i used `` for column name though im getting error...
my code is
$sql = "INSERT INTO order(`pcode`) VALUES ('$pcode')";
if(!mysql_query($sql,$con))
die('cant connect ' .mysql_error());
Order is a reserved word for the "ORDER BY" clause
try
"INSERT INTO `order`(pcode) VALUES ('$pcode')";
Note: Please ensure $pcode is being run through mysql_real_escape_string, or better yet look into the PDO extension and their prepared queries
if order is your table name and pcode is your column name then you can use this:
$sql = sprintf("INSERT INTO `order` (pcode) VALUES('%s')", $pcode);

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());

Weird behavior PHP vs MySQL

This is the SQL:
TRUNCATE TABLE `dc_path`;
INSERT INTO dc_path (coords) VALUES('(40.64406436923055, -8.638539251709062)');
INSERT INTO dc_path (coords) VALUES('(40.62791121610622, -8.615193304443437)');
INSERT INTO dc_path (coords) VALUES('(40.62895347295352, -8.6625718444825)');
If I try to execute that query on phpmyadmin it works just fine, but through php it gives me this 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 dc_path (coords) VALUES('(40.64406436923055, -8.638539251709062)');I' at line 1
I tried many things and I can't work it out!
Thanks in advance.
Edit:
PHP
function insertPath($coords){
$conn = connectDB();
$coords = explode(";",$coords);
$sql = "";
$sql = "TRUNCATE TABLE `dc_path`; ";
for($i=0;$i<count($coords)-1;$i++){
$sql .= "INSERT INTO dc_path (coords) VALUES('".$coords[$i]."');";
}
echo $sql;
$query = mysql_query($sql, $conn) or die(mysql_error());
closeDB($conn);
return true;
}
the $coords variable contains something like these values:
(40.638854101691635, -8.6515855163575);(40.629474595277166, -8.63235944213875);
You cannot perform several queries in one mysql_query() call.
So split that string to 4 separated queries (without ; in the end) and everything will work
Don't use the old mysql_connect API, use mysqli - which supports multiple statements in one.
Read more about the different PHP - mySQL apis here: http://www.php.net/manual/en/mysqlinfo.api.choosing.php
There it says that the old mysql API is not recommended for new projects, and that long term deprecation has been announced.
What function are you using to run this? If you're using mysql_query then you can only do one query at a time, however you can merge the insert statements into one like
INSERT INTO dc_path (coords) VALUES
('(40.64406436923055, -8.638539251709062)'),
('(40.62791121610622, -8.615193304443437)'),
('(40.62895347295352, -8.6625718444825)');
function insertPath($coords){
$conn = connectDB();
$coords = explode(";",$coords);
mysql_query("TRUNCATE TABLE `dc_path`", $conn);
for($i=0;$i<count($coords)-1;$i++){
mysql_query("INSERT INTO dc_path (coords) VALUES('".$coords[$i]."')", $conn);
}
closeDB($conn);
return true;
}
You cannot query more than one statement using mysql_query().
Query like this
for($i=0;$i<count($coords)-1;$i++){
$sql = "INSERT INTO dc_path (coords) VALUES('".$coords[$i]."');";
$query = mysql_query($sql, $conn) or die(mysql_error());
}

Categories