Insertion failed error in MySQL - php

while($row=mysql_fetch_array($result2)){
//return $row['ProjectID'];
$sql="INSERT INTO `tycodashboard` (ProjectID,DesignationID,ReqcompID,IntOrgID,FinishedTimeID,ProjectStatusID,PhaseID
) VALUES('{$row['ProjectID']}','$pm,'$req','$initiating,'$initiating','$ftime,'$ProjectStatus,'$Phase)";
$result=mysql_query($sql);
if(!$result){
if(mysql_errno() == ER_DUP_ENTRY){
throw new Exception("INSERT FAILED.\n\nThe database already contains a Project with the Project Name \"$ldesc\", please pick another.");
}else{
throw new Exception("INSERT FAILED.\n\n".mysql_error());
}
}
}//exits
INSERT FAILED.
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 '3','2,'2','2,'2,'3)' at line 2

You are missing a whole bunch of quotes as you can see from the error message:
'3','2,'2','2,'2,'3
Try adding the quotes where they are missing and see if that helps:
$sql="INSERT INTO `tycodashboard` (ProjectID,DesignationID,ReqcompID,IntOrgID,FinishedTimeID,ProjectStatusID,PhaseID
) VALUES ('{$row['ProjectID']}','$pm','$req','$initiating','$initiating','$ftime','$ProjectStatus','$Phase')";

Related

PDOExceptionERROR 42000 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right

I'm using the wamp server but unfortunately, I face a problem that I can't solve. Please someone find the error from my code and tell me How can I solve it?
##While I am trying to get data from the request id by PDO method, I'm getting the following errors##
<?php require_once('header.php'); ?>`
<?php
if (isset($_POST['form1']))
{
$q = $pdo->prepare("UPDATE slider SET
slider_title=?,
slider_subtitle=?,
slider_buttontext=?,
slider_buttonurl=?,
WHERE slider_id=?
");
$q->execute(array(
$_POST['slider_title'],
$_POST['slider_sub_title'],
$_POST['slider_button_text'],
$_POST['slider_button_url'],
$_REQUEST['id']
));
$success_message = "Slider Information Is Updated Successfully";
}
?>
<?php
$q= $pdo->prepare("SELECT * FROM slider WHERE slider_id=?");
$q->execute([$_REQUEST['id']]);
$res= $q->fetchAll();
foreach ($res as $row) {
$slider_title = $row['slider_title'];
$slider_subtitle = $row['slider_subtitle'];
$slider_buttontext = $row['slider_buttontext'];
$slider_buttonurl = $row['slider_buttonurl'];
$slider_photo = $row['slider_img'];
}
?>
The browser output is like the below text
Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 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 'WHERE slider_id='9'' at line 6 in F:\wamp\www\hotel\admin\slider_edit.php on line 18
don't use comma in last field in sql statment
if (isset($_POST['form1']))
{
$q = $pdo->prepare("UPDATE slider SET
slider_title=?,
slider_subtitle=?,
slider_buttontext=?,
slider_buttonurl=?, // <- here - remove this comma
WHERE slider_id=?

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version

I am trying to update a row in mysql, however, error no 1024 comes up every time
#$name= $_POST['name'];
#$bio=$_POST['bio'];
#$email=$_POST['email'];
if(!empty($_POST['name']) && !empty($_POST['bio']) && !empty($_POST['email']) )
{
$result="SELECT * FROM accounts where email='$email'";
$row = mysqli_fetch_array(mysqli_query($con,$result),MYSQLI_ASSOC);
$row['id']=$id;
$Sql_Query = mysqli_query($con,"UPDATE profile SET name= '$name', bio = '$bio' WHERE id = '$id'");
if(mysqli_query($con,$Sql_Query)){
echo 'Record Updated Successfully';
}
else{
echo 'Something went wrong, whether id is not present or something else'.mysqli_error($con);
}
}else
{
echo 'missing parameters';
}
error
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 '1' at line 1
Any help will be deeply appreciated
(See the edit near the bottom).
What happened here is that you executed the same query twice for the UPDATE and the error that you should be getting is a "1". (This before the edit).
Change your
$Sql_Query = mysqli_query($con,"UPDATE profile SET name= '$name', bio = '$bio' WHERE id = '$id'");
// ^^^^^^^^^^^^^^^^^
if(mysqli_query($con,$Sql_Query)){
// ^^^^^^^^^^^^^^^^^
echo 'Record Updated Successfully';
}
to just
if($Sql_Query){
echo 'Record Updated Successfully';
}
and use a prepared statement to help against an SQL injection.
https://en.wikipedia.org/wiki/Prepared_statement (General information).
http://php.net/manual/en/mysqli.prepare.php (MySQLi_).
http://php.net/manual/en/pdo.prepared-statements.php (PDO).
As per your edit where you added:
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 '1' at line 1
...which is what I suspected and posted in a comment earlier.
By the way; those # characters are error suppressors and should be removed during development.
Using PHP's error reporting would help to a certain extent, but not for those #'ed variables for the POST arrays should there be anything wrong for them.
Edit:
As stated in a comment given by IncredibleHat, this line:
$row['id']=$id;
is reversed and should be written as:
$id = $row['id'];
The id is to be assigned "to" the row and not the other way around.
I failed to see that, my bad. Good catch on that.

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 ''soft_name'

I am having this error in the following code. Please tell me my mistake.
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 ''soft_name', 'soft_ver', 'soft_size', 'sdesc', 'slink') VALUES ('h', ' ', ' ', '' at line 1
Code
<?php
$sname=$_POST['name'];
if (!empty($_POST['ver'])) $ver=$_POST['ver']; else $ver=" ";
if (!empty($_POST['ssize'])) $ssize=$_POST['ssize']; else $ssize=" ";
if (!empty($_POST['description'])) $desc=$_POST['description']; else $desc=" ";
if (!empty($_POST['link'])) $slink=$_POST['link']; else $slink=" ";
mysql_select_db('1027593',mysql_connect('','',''));
$qry="INSERT INTO downloads ('soft_name', 'soft_ver', 'soft_size', 'sdesc', 'slink') VALUES ('".$sname."', '".$ver."', '".$ssize."', '".$desc."', '".$slink."')";
mysql_query($qry);
echo mysql_error();
die();
?>
Change Query
$qry="INSERT INTO downloads ('soft_name', 'soft_ver', 'soft_size', 'sdesc', 'slink') VALUES ('".$sname."', '".$ver."', '".$ssize."', '".$desc."', '".$slink."')";
to
$qry="INSERT INTO downloads (`soft_name`, `soft_ver`, `soft_size`, `sdesc`, `slink`) VALUES ('".$sname."', '".$ver."', '".$ssize."', '".$desc."', '".$slink."')";
your mistake is you add a single quote(') in field name and it is not allow in sql field.
hope this solution help for you

PHP file showing syntatical error

I'm trying to send title and content to my table named write . But i'm having error while running this php file .
Below is the error shown
not sucessfullyYou have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'write values('','','')' at line 1
<?php
require "connection.php";
$id = $_POST["id"];
$title =$_POST["title"];
$content =$_POST["content"];
$sql_query ="insert into write values('$id','$title','$content');";
if(mysqli_query($con,$sql_query))
{
echo"data insertion sucess";
}
else
{
echo "not sucessfully".mysqli_error($con);
}
?>
WRITE is a reserved keyword in MySQL so you should escape it using backticks:
insert into `write` ...
But I would recommend you rename the table into something more sensible.

MYSQL error while inserting data using php

I am practicing php and sql. at a stage when I'm trying to enter a record into a table with 2 exiting records. but it doesn't add and show an 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 '=('Aqeela','Nasreen','Hakeem Chattah')' at line 1"
Why is it not entering a record in data base. Is there any syntax error?
$username="root";
$pass="";
$database="addressbook";
$server="127.0.0.1";
$con=mysql_connect($server,$username,$pass);
$db_found=mysql_select_db($database,$con);
if($db_found)
{
$sql_insert="INSERT INTO table_address_book(f_name,l_name,address) VALUES=('Aqeela','Nasreen','Hakeem Chattah')";
$result=mysql_query($sql_insert);
if(!$result){
print "sorry cannot proceed your request<br>".mysql_error();
}
else
{
// print "recorded entered successfuly<br>";
// print "now dATABASES AFTER EDITING ARE<BR><br>";
$new_sql="SELECT*FROM table_address_book";
$result_after_editing=mysql_query($new_sql);
while($db_field_edited=mysql_fetch_assoc($result_after_editing))
{
print $db_field_edited['ID']."<br>";
print $db_field_edited['f_name']."<br>";
print $db_field_edited['l_name']."<br>";
print $db_field_edited['address']."<br>";
print "<BR><BR><BR>";
}
mysql_close($con);
}
}
else
{
die("unable to connect database ".mysql_error());
}
The error clearly shows place where error in syntax occur.
Remove that =
INSERT INTO table_address_book(f_name,l_name,address) VALUES('Aqeela','Nasreen','Hakeem Chattah')"
I think there is an error in your INSERT INTO statment, you have written wrong VALUES part.
$sql_insert="INSERT INTO table_address_book(f_name,l_name,address) VALUES=('Aqeela','Nasreen','Hakeem Chattah')";
you need to remove "=" from your VALUES= part like this.
$sql_insert="INSERT INTO table_address_book(f_name,l_name,address) VALUES('Aqeela','Nasreen','Hakeem Chattah')";
please correct this line of code in your code and check it again.
Remove the = sign from VALUES=(...)
There's no '=' after VALUES, just:
VALUES (val1, val2, .., valN)

Categories