My string field won't insert into my database.
(The columns follower_username and following_username they are VARCHAR(200) don't insert )
The: follower and following column values insert work.
mysql_query("INSERT INTO `follow` (`follower`, `following`, `follower_username`, `following_username`) VALUES ('".$userid."', '".$get_user_id."', '".$username."', '".$get_user."')");
Strings:
$get_user = mysql_real_escape_string($row['username']);
$get_user_id = (int)mysql_real_escape_string($row['id']);
$userid = (int)mysql_real_escape_string($user_data['id']);
$username = mysql_real_escape_string($user_data['username']);
I have no idea what to do, whether it is the PHP or the database itself :S
Thanks in advance :)
You could try echoing the mysql statement just before the mysql_query, i.e.
echo "INSERT INTO `follow` (`follower`, `following`, `follower_username`, `following_username`) VALUES ('".$userid."', '".$get_user_id."', '".$username."', '".$get_user."')";
and check if the string is what you expected it to be. If it is what you expected, try manually copying the string and pasting it into the mysql console and see if any errors occur.
try this :
mysql_query("INSERT INTO follow (`follower`, `following`, `follower_username`, `following_username`) VALUES ('".$userid."', '".$get_user_id."', '".$username."', '".$get_user."')");
don't use single quotes around table name.
Try adding mysql_error to your statement to find out what error is it so you can fix it:
mysql_query("INSERT INTO `follow` (`follower`, `following`, `follower_username`,
`following_username`) VALUES ('".$userid."', '".$get_user_id."', '".$username."',
'".$get_user."')") or die (mysql_error());
For debug and simple work I recommended you store SQL query in variable.
$query = "INSERT INTO `follow` (`follower`, `following`, `follower_username`, `following_username`) VALUES ('".$userid."', '".$get_user_id."', '".$username."', '".$get_user."')";
echo "DEBUG:".$query;
mysql_query($query);
Try this:
$objQuery = mysql_query("INSERT INTO `follow` (`follower`, `following`, `follower_username`,
`following_username`) VALUES ($userid, $get_user_id, '".$username."',
'".$get_user."')") or die (mysql_error());
if(!$objQuery){
echo "something went wrong!";
}
Related
I'm looking to insert values from two sources (variables and a field from another table) into a new table. After some research, I found that this was possible, but cannot figure out how to accomplish this with my query.
Let me know if I have not provided enough context or code.
//Query to INSERT data
$query3 = "INSERT INTO `Checked_Out` (`name`, `quantityCheckedOut`, `checkedOut`, `returnDate`, `image`, `ID`) VALUES ('$name', '$quantityTaken', '$checkedOut', '$returnDate', '$ID')
SELECT `image` FROM `Checked_In` WHERE `ID` = '$ID'";
Try this:
$query3 = "INSERT INTO `Checked_Out` (`name`, `quantityCheckedOut`, `checkedOut`, `returnDate`, `image`, `ID`)
SELECT '$name', '$quantityTaken', '$checkedOut', '$returnDate', `image`, '$ID' FROM `Checked_In` WHERE `ID` = '$ID'";
I am really new to php and I am trying to use simple insert to my mysql database from the form.
I know that this mysql connection/insertion is dangerous and not used anymore. so can anyone please help me with this simple thing? I tried to google, but nothing is working so far :/
<?
$text=$_POST['name'];
$text=$_POST['surename'];
mysql_connect("localhost", "db_name", "pass") or die(mysql_error());
mysql_select_db("db_name") or die(mysql_error());
$result = mysql_query("INSERT INTO `table` (name, surename)
VALUES (NOW(), '".mysql_real_escape_string($name)."', '".mysql_real_escape_string($surename)."')");
if (!$result) {
die('Invalid query: ' . mysql_error());
}
?>
Maybe change
$text=$_POST['name'];
$text=$_POST['surename'];
to
$name = $_POST['name'];
$surename = $_POST['surename'];
PS: And also your column names don't match your values. Your query, after inserting params
"INSERT INTO `table` (name, surename) VALUES (NOW(), '".mysql_real_escape_string($name)."', '".mysql_real_escape_string($surename)."')"
will probably look like this
INSERT INTO `table` (name, surename) VALUES (NOW(), 'Jhon', 'Wick')
As you can see there's name, surename (which probably should be surname) and (NOW(), 'Jhon', 'Wick'). So either add a column (if you have that column in your database):
INSERT INTO `table` (created_at, name, surename) VALUES (NOW(), 'Jhon', 'Wick')
or remove NOW() from your values
INSERT INTO `table` (name, surename) VALUES ('Jhon', 'Wick')
i'm making a query to insert some values to a table and update other in other table all this in the same function.
The problem i have is with the Insert query, when has been execute, give me a syntax error that you can see below.
I reviewed the code many times but i don't see any error. When I remove the Insert query works fine and if I remove de update query and leave just the insert give me the same error.
Here the query
if (isset($_POST['goodalt']) && isset($_POST['gengood'])){
$goodalt = mysqli_real_escape_string($con, $_POST['goodalt']);
$genid = mysqli_real_escape_string($con, $_POST['gengood']);
$res = mysqli_query($con, "SELECT * FROM `generators` WHERE `name` = '$genid'") or die(mysqli_error($con));
while($row = mysqli_fetch_assoc($res)) {
$genname = $row['name'];
}
mysqli_query($con, "INSERT INTO `user_accounts` (`username`, `accs`, `genid`, `gen-name`, `date`, `status`) VALUES ('$username', '$goodalt', '$genid', '$genname', '$date', '1'") or die(mysqli_error($con));
mysqli_query($con, "UPDATE `generator$genid` SET `status` = '3' WHERE `alt` = '$goodalt'") or die(mysqli_error($con));
}
Any help? Thanks.
Apart form error, there is a BIG issue about concurrency and threading model.
Every time you split INSERT/UPADATE in more separate PHP calls two mysql, you can analyze CAREFULLY if separate queries maintain DB consistent.
Consider carefully is two (or more) web requests (executing the same PHP script) can be safely run in parallel, especially on code where You did:
mysqli_query($con, "INSERT INTO `user_accounts` (`username`, `accs`, `genid`, `gen-name`, `date`, `status`) VALUES ('$username', '$goodalt', '$genid', '$genname', '$date', '1'") or die(mysqli_error($con));
mysqli_query($con, "UPDATE `generator$genid` SET `status` = '3' WHERE `alt` = '$goodalt'") or die(mysqli_error($con));
Insert and Updates are atomic, but here you call two separate queries
Your missing a close bracket in your insert...
mysqli_query($con, "INSERT INTO `user_accounts` (`username`, `accs`, `genid`, `gen-name`, `date`, `status`) VALUES ('$username', '$goodalt', '$genid', '$genname', '$date', '1')") or die(mysqli_error($con));
BUT you should also be using prepared statements and bind variables...
The issue that you're facing is because of the unnecessary usage of backticks or single quotation marks. You can follow the following article to make the changes and the issue should be sorted out.
Article here
Change these from:
mysqli_query($con, "INSERT INTO `user_accounts` (`username`, `accs`, `genid`, `gen-name`, `date`, `status`) VALUES ('$username', '$goodalt', '$genid', '$genname', '$date', '1'") or die(mysqli_error($con));
to
mysqli_query($con, "INSERT INTO user_accounts (username, accs, genid, gen-name, date, status) VALUES ('$username', '$goodalt', '$genid', '$genname', '$date', '1'") or die(mysqli_error($con));
Hope that helps!
I have the following query which is working fine in inputting the arrays $stid and $attendance_status. However, I also want to populate the column called SCH_TIMESLOT with a fixed number 22 or eventually a string value which is the same for all records. I am getting an error stating:
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 '22' at line 2
This is my query:
for($i=0;$i<count($attendance_status);$i++){
mysql_query("INSERT INTO `ATTENDANCE` (`ID`, `STUDENT_ID`, `ATTENDANCE`, `SCH_TIMESLOT`) VALUES
(NULL, $stid[$i], $attendance_status[$i]), 22") or die (mysql_error());
Thank you for your help
You have missed out 22 from your VALUES brackets. It has to be inside.
<?php
for($i=0;$i<count($attendance_status);$i++){
mysql_query("INSERT INTO `ATTENDANCE` (`ID`, `STUDENT_ID`, `ATTENDANCE`, `SCH_TIMESLOT`) VALUES
(NULL, $stid[$i], $attendance_status[$i], 22)") or die (mysql_error());
You have a syntax error.
$stid[$i], $attendance_status[$i]), 22") you are closing the brackets before 22
Try it like this
"INSERT INTO `ATTENDANCE` (`ID`, `STUDENT_ID`, `ATTENDANCE`, `SCH_TIMESLOT`)
VALUES
(NULL, $stid[$i], $attendance_status[$i], 22)"
Try this, i replace ")" before "22" and move it after.
for($i=0;$i<count($attendance_status);$i++){
mysql_query("INSERT INTO `ATTENDANCE` (`ID`, `STUDENT_ID`, `ATTENDANCE`, `SCH_TIMESLOT`) VALUES
(NULL, $stid[$i], $attendance_status[$i], 22)") or die (mysql_error());
Change your sql query as below
for($i=0;$i<count($attendance_status);$i++){
mysql_query("INSERT INTO `ATTENDANCE` (`ID`, `STUDENT_ID`, `ATTENDANCE`, `SCH_TIMESLOT`) VALUES `(NULL, $stid[$i], $attendance_status[$i], 22") or die (mysql_error());`
i have a db query in php that is not inserting into database. Have used this format lots of times but for some reason its not working now. any ideas please
$query = "INSERT INTO `databasename`.`member_users` (`id`, `first_name`, `last_name`, `username`, `password`, `address1`, `address2`, `postcode`, `access`, `expires`) VALUES (NULL, '$fname', '$lname', '$email', '', '$add1', '$add2', '$postcode', '0', '')";
$result = mysql_query($query);
if($result){
echo"query inserted";
}else{
echo "nope";
}
Instead of echo "nope"; I suggest something like :
echo 'error while inserting : ['.mysql_errno().'] '.mysql_error();
echo 'query : '.$query;
This way you will be able to see the exact error and the query that was executed.
It can be a lot of things :
Constraint error with a foreign key
Data type error
Non-existent field
Wrong database or table name
Instead of...
$query = "INSERT INTO `databasename`.`member_users` ..."
do
$query = "INSERT INTO member_users ..."
Hope it works. :)
If databasename and member_users are variables then,
Instead of
$query = "INSERT INTO databasename.member_users...
do
$query = "INSERT INTO $databasename.$member_users...