I'm getting the same error some some people got here. But still my problem was not solved. In my University project for my all modules Im getting the same error.
When I'm running the following query in PHP
$username = $_SESSION['username'];
$query = "SELECT id FROM user WHERE username = '$username'";
$qpost = mysqli_query($connection,$query);
$post_user = mysqli_fetch_assoc($qpost);
$forum_size = strlen($forum);
$dt = time();
$datetime = strftime("%Y-%m-%d %H:%M:%S", $dt);
$enteruser = $post_user['id'];
$Query = "INSERT INTO posts('id','user_id','post','date_added') VALUES(NULL,'$enteruser','$forum','$datetime')";`
I'm getting the following 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 ''id','user_id','post','date_added') VALUES (NULL,'3','erye yeyeyeyetyery ery ery '' at line 1
And when I was run it in mysql shell I'm getting the same error.
When I run that query in phpmyadmin as following,
INSERT INTO posts('id','user_id','post','date_added') VALUES (NULL,3,'erye yeyeyeyetyery ery ery ','2015-07-24 18:53:48');
I'm getting the same error. Can you please help me to solve this.I checked the query in every way I can adn error remains. I cant go forward withour solving this! Thank you for your help!
You don't need apostrophes on your table headers.
$username = $_SESSION['username'];
$query = "SELECT id FROM user WHERE username = '$username'";
$qpost = mysqli_query($connection,$query);
$post_user = mysqli_fetch_assoc($qpost);
$forum_size = strlen($forum);
$dt = time();
$datetime = strftime("%Y-%m-%d %H:%M:%S", $dt);
$enteruser = $post_user['id'];
$Query = "INSERT INTO `posts` (id, user_id, post, date_added) VALUES (NULL,'$enteruser','$forum','$datetime')";
Given id is primary-key and auto-increment, you can exclude it. You can also use native Mysql function now() for date_added
$Query = "INSERT INTO `posts` (user_id, post, date_added) VALUES ('$enteruser','$forum',now())";
Try this...
$username = $_SESSION['username'];
$query = "SELECT id FROM user WHERE username = '$username'";
$qpost = mysqli_query($connection,$query);
$post_user = mysqli_fetch_assoc($qpost);
$forum_size = strlen($forum);
$dt = time();
$datetime = strftime("%Y-%m-%d %H:%M:%S", $dt);
$enteruser = $post_user['id'];
$Query = "INSERT INTO posts ('user_id','post','date_added') VALUES('$enteruser','$forum','$datetime')";`
Related
The majority of this code works just fine, the database get updated when I click on the button, but the last query (the UPDATE one) doesn't execute for some reason.
I tried turning on mysql log on phpmyadmin, but even there it's not executed.
It doesn't show me any error, and I really don't know what could be wrong.
$query = "SELECT username, coins FROM users WHERE userid='$userid' LIMIT 1";
$result = mysqli_query($db, $query);
$user = mysqli_fetch_assoc($result);
$_SESSION['username'] = $user['username'];
$_SESSION['coins'] = $user['coins'];
$op = $user['username'];
$op = mysqli_real_escape_string($forumdb, $op);
$postcontent = $_POST['postcontent'];
$postcontent = mysqli_real_escape_string($forumdb, $postcontent);
$posttitle = $_POST['posttitle'];
$posttitle = mysqli_real_escape_string($forumdb, $posttitle);
$sectionid = $_GET['sectionid'];
$sectionid = mysqli_real_escape_string($forumdb, $sectionid);
$query = "INSERT INTO topic (section_id, name, replies, op, lastpost, lastuserid, views, sticked) values('$sectionid', '$posttitle', '0','$op', CURRENT_TIMESTAMP(),'$userid', '0', '0')";
$result = mysqli_query($forumdb, $query) or trigger_error("Query Failed! SQL: $query - Error: ".mysqli_error($forumdb), E_USER_ERROR);
$last_id = mysqli_insert_id($forumdb);
$query = "INSERT INTO posts (topic_id, content, user_id) values('$last_id', '$postcontent', '$userid')";
mysqli_query($forumdb, $query);
$query = "UPDATE section SET lastpost='$username', threads=threads+1, posts=posts+1 WHERE id='$sectionid'";
mysqli_query($forumdb, $query) or trigger_error("Query Failed! SQL: $query - Error: ".mysqli_error($forumdb), E_USER_ERROR);
You can use the following to solve your problem:
"UPDATE `section` SET `lastpost`='$username', `threads`=threads+1, `posts`=posts+1 WHERE `id`='$sectioni
It would have been helpful if you explained what "doesn't execute for some reason" means.
Either you get an error indicating that the SQL was invalid at runtime or the execution continued and no data was changed.
Without knowing what the error was, we can't advise what would have caused an error. If execution continued, but the record was not (obviously) updated, then it must be because the WHERE clause of the update statement did not match any rows. You could verify this by checking mysqli_affected_rows().
The queries you have run previously wil be in the mysql general log. You might want to
echo the SQL statement to the output and check that it is populated as you expect
I have a Mysql Database named user. Here is a picture:
I want to change the Username of the user "dodlo.rg" programmatically.
Actually, I have the PHP-Version 7.1. And this is a part of my PHPCode:
EDITED CODE:
$newName= $_POST["changeT"];
$userId = $_POST["userId"];
$db = mysqli_connect("trolö", "trolö", "trolö123", "trolö")
$sql = "UPDATE user SET username = '$newName' WHERE user_id = '$userId'";
$query = mysqli_query($db, $sql);
$response["successU"] = true;
But I get the Error: "You gave an Error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT * FROM user' at line 1"
Thanks in advance.
The problem lies in 2 parts.
Firstly, since this column is a varchar field it needs to be inside quotes else it produces an sql error.
Secondly the SELECT statement just after is not valid, but i guess it was a copy/paste error.
Therefore your working code should be:
$newName= $_POST["changeT"];
$db = mysqli_connect("trolö", "trolö", "trolö123", "trolö")
$sql = "UPDATE user SET username = '".addslashes($newName)."' WHERE username = 'dodlo.rg'";
$query = mysqli_query($db, $sql);
$response["successU"] = true;
Also, please consider using your primary keys on your where statement rather a varchar field, as it'll improve speed when more complex queries. (eg. where user_id = 35 instead of where username = 'dodlo.rg' ).
Lastly, but quite important this code might be vulnerable to sql injections. You need to use prepared statements.
You have to convert this query into two parts
$sql1 = "UPDATE user SET username = $newName WHERE username = 'dodlo.rg'";
$sql2 = "SELECT * FROM user";
I am trying to build an SQL query that will insert the check-in time for a child at a fictional daycare facility. Here is a condensed version of my query code:
$childFirstName = $_POST['childFirstName'];
$childLastName = $_POST['childLastName'];
$now = new DateTime();
$nowDate = $now->format('m-d-Y');
$nowTime = $now->format('h:i');
$sql_childID = "SELECT id FROM child
WHERE firstName = '$childFirstName'
AND lastName = '$childLastName'";
$result = $pdo->query($sql_childID);
$row = $result->fetch();
$sql = "INSERT INTO checkinout(date, in, child_id) VALUES(?,?,?)";
$statement = $pdo->prepare($sql);
$statement->bindValue(1, $nowDate);
$statement->bindValue(2, $nowTime);
$statement->bindValue(3, $row['id']);
$statement->execute();
The checkinout table uses VARCHAR datatypes for the date and in columns. Originally they were set to use DATETIME, but I received the same errors.
Right now I get the following errors returned...
You can see from the error messages that my values are getting passed in the way I want them to, but I don't understand where my syntax error would be.
Enclose your field names with backticks. Two of them are reserved words (date and in):
$sql = "INSERT INTO checkinout(`date`, `in`, `child_id`) VALUES(?,?,?)";
https://dev.mysql.com/doc/refman/5.5/en/keywords.html
I've been trying to get this INSERT to work correctly, so I worked through the undefined variable and index problems and now I think I am nearly there.
Below is the code:
<?php
session_start();
require "../dbconn.php";
$username = $_SESSION['username'];
$query1 = "SELECT user_table.user_id FROM user_table WHERE user_table.username ='".$username."'";
$query2 = "SELECT department.department_id FROM department, user_table, inventory
WHERE user_table.user_id = department.user_id
AND department.department_id = inventory.department_id";
//Copy the variables that the form placed in the URL
//into these three variables
$item_id = NULL;
$category = $_GET['category'];
$item_name = $_GET['item_name'];
$item_description = $_GET['item_description'];
$item_quantity = $_GET['quantity'];
$item_quality = $_GET['quality'];
$item_status = NULL;
$order_date = $_GET['order_date'];
$invoice_attachment = NULL;
$edit_url = 'Edit';
$ordered_by = $username;
$user_id = mysql_query($query1) or die(mysql_error());
$department_id = mysql_query($query2) or die(mysql_error());
$price = $_GET['price'];
$vat = $_GET['vat%'];
$vat_amount = $_GET['vat_amount'];
$create_date = date("D M d, Y G:i");
$change_date = NULL;
//set up the query using the values that were passed via the URL from the form
$query2 = mysql_query("INSERT INTO inventory (item_id, category, item_name, item_description, item_quantity, item_quality, item_status, order_date,
invoice_attachment, edit_url, ordered_by, user_id, department_id, price, vat, vat_amount, create_date, change_date VALUES(
'".$item_id."',
'".$category."',
'".$item_name."',
'".$item_description."',
'".$item_quantity."',
'".$item_quality."',
'".$item_status."',
'".$order_date."',
'".$invoice_attachment."',
'".$edit_url."',
'".$ordered_by."',
'".$user_id."',
'".$department_id."',
'".$price."',
'".$vat."',
'".$vat_amount."',
'".$create_date."',
'".$change_date."')")
or die("Error: ".mysql_error());
header( 'Location:../myorders.php');
?>
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 'VALUES( '', 'adasd', 'dsadsa', 'dsad', 'sadsad', '' at line 2
Could anyone please let me know where I am going wrong? :(
Been staring at this for 3-5 hours already :(
You are not actually trying to insert any data into your table. You only craft and assign the query in string form to a variable. You need to use the function mysql_query to actually run the code.
As pointed out you will also have to specify the columns you are inserting data into in the MySQL query if you don't supply data for every column (in the correct order). Here you can look at the MySQL insert syntax.
I would also urge you to look into using the MySQLi or the MySQL PDO extensions for communicating with your MySQL database since the MySQL extension is deprecated. Look here for additional information and comparisons.
Here, you only assign the values to the $query var:
$query = "INSERT INTO inventory VALUES (
'".$item_id."',
'".$category."',
'".$item_name."',
'".$item_description."',
'".$quantity."',
'".$quality."',
'".$item_status."',
'".$order_date."',
'".$invoice_attachment."',
'".$edit_url."',
'".$ordered_by."',
'".$price."',
'".$vat."',
'".$vat_amount."',
'".$create_date."',
'".$change_date."')"
or die("Error: ".mysql_error());
You do not actually run the query.
try:
$query = mysql_query("INSERT INTO inventory (column_name1, column_name 2, column_name3 ... the column name for each field you insert) VALUES (
'".$item_id."',
'".$category."',
'".$item_name."',
'".$item_description."',
'".$quantity."',
'".$quality."',
'".$item_status."',
'".$order_date."',
'".$invoice_attachment."',
'".$edit_url."',
'".$ordered_by."',
'".$price."',
'".$vat."',
'".$vat_amount."',
'".$create_date."',
'".$change_date."')")
or die("Error: ".mysql_error());
Also, you should use mysqli_* or any other PDO as the mysql_* functions are deprecated
If you are not inserting in all columns you need to specify the columns you are going to insert. Like this:
INSERT INTO Table(Column1, Column6) VALUES (Value1, Value6)
You are missing the column names in your INSERT
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