mysql_query("INSERT INTO new_emails (from,subject,message,to,filename,fileurl) VALUES ('$id','$stripsubj','$content','$toids','$upload_name','$att')");
Firstly, I am aware that mysql_query is depreciated, and I will be re-doing the whole script at some point in the near future to accommodate this.
My main problem is that this query is not currently inserting anything into the database, and I haven't the faintest clue why. Unfortunately I don't have access to any phpMyAdmin logs/SQL logs that can help me debug this problem.
I have been working on this for a while so I am hoping I have something pretty stupid and not noticed.
The columns inside "new_emails" are: id,from,savedtodb,subject,message,to,filename,fileurl
The ID is auto_increment, and savedtodb is CURRENT_TIMESTAMP, which is why I am leaving them out.
Have I done anything particularly stupid or is there a deeper reason why this isn't working?
Cheers in advance!
from and to are reserved words. You need backticks for them:
INSERT INTO new_emails (`from`, subject, message, `to`, filename, fileurl)
VALUES ('$id', '$stripsubj', '$content', '$toids', '$upload_name', '$att')
There are some possible errors:
1) You may not have selected the database using mysql_selct_db or while declaring the connection.
2) Maybe you didn't escape the single quotes. MySQL hates single quotes, and would make an error if it finds any in the input.
Related
This code is to use in WordPress plugin.
The following is my code I am using to insert data from CSV file to database:
$sql="LOAD DATA LOCAL INFILE '".$fileurl."' INTO TABLE ".$table_name."
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\r\n' IGNORE 1 LINES (`first_name`,`last_name`, `email`, `mobile_phone`, `address_1`, `address_2`, `city`, `state`, `zip`, `about_us` );
";
$query = $wpdb->query($sql);
When I do this var_dump($query); it shows int(0), and data is successfully inserted in table. My question is how can I get number of inserted rows?
You can find the affected rows in your query using below.
$count = $wpdb->query($sql);
$count is your affected rows.
Very old question, I know, and perhaps this answer is trivially obvious, but posting in the hope it might be useful to someone who stumbled across it as I did.
In this particular case with LOAD DATA, one option might be to simply run an sql COUNT() before and after the LOAD DATA, and then take the difference:
$count_before = $wpdb->query("SELECT COUNT(*) FROM $table_name");
// LOAD DATA ...
$count_after = $wpdb->query("SELECT COUNT(*) FROM $table_name");
$new_rows = $count_after - $count_before;
I understand this may not work well if there is other simultaneous activity on the table; though you may be able to lock the table to prevent conflicting updates and to make the LOAD DATA even faster. You might be able to get this with $wpdb->affected_rows and that's also worth checking.
No doubt since it's 2 years ago since you asked this, you have any number of other working solutions; hopefully this is useful to anyone googling for a solution here.
Object $wpdb contains a lot of useful things. You can watch this by dump this object to see public properties.
One of them is what you search:
echo $wpdb->affected_rows;
You cannot get this information using a LOAD DATA statement with wpdb::query(). It doesn't understand the statement, so doesn't automatically fetch the number of affected rows. You can't get it yourself, either, because the necessary wpdb class properties (use_mysqli, dbh) are not public.
I need serious help my my sql statement. First, for some reason the default values for bio and user_image are not inserting. Second, my statement works on and off. Sometimes it inserts, sometimes it doesn't. I need some smart person help. Thanks in advance. Heres my code:
$query = "INSERT INTO users VALUES ('{$_POST['display_name']}', '{$_POST['email']}','{$_POST['password']}','active','{$_POST['first_name']}','{$_POST['last_name']}',DEFAULT,DEFAULT)";
mysql_query($query);
echo '<META HTTP-EQUIV="Refresh" Content="0; URL=success.php">';
exit;
Messed up I know, im doing this project to learn more about databases. Heres my db structure:
Field Type Null Default
id int(11) No
username varchar(255) No
email varchar(255) No
password varchar(255) No
status enum('active', 'inactive') No
first_name text No
last_name text No
bio varchar(305) No HEY! theres nothing here yet ....Complete your bio if you want to get rid of this lame placeholder text. Use this space on your page to tell the internet a little bit about yourself. Or just make everyone feel bad by listing all of your amazing accomplishments! I prefer the latter.
user_image varchar(305) No user_profile.jpg
**Changed but still not working:
$query = "INSERT INTO users(username, email, password, first_name,last_name)
VALUES ({$_POST['display_name']}, {$_POST['email']}, {$_POST['password']}, {$_POST['first_name']}, {$_POST['last_name']})";
Your query is fundamentally unsafe - it can be exploited to insert malicious statements, and POOF! your whole database is gone. You really, really need to read this SO thread and use PDO or mysqli instead, or if that's absolutely not an option at the very least use mysql_real_escape_string.
Having said that, since you're not doing any escaping, any string with a ' in it will break your query. Let's say that $_POST['display_name'] is set to Frank's Restaurant. Your query will then start with INSERT INTO users VALUES ('Frank's Restaurant', and MySQL will think that the query stopped erroneously after Frank, since that's followed by a '.
In addition, as Glavić suggested, you need to add a field for the id, or specify fields manually.
Firstly try this:
$query = "INSERT INTO users VALUES ('{$_POST['display_name']}', '{$_POST['email']}','{$_POST['password']}','active','{$_POST['first_name']}','{$_POST['last_name']}',null,null)";
If it wouldn't help try to specify what fields you want to insert. Anyway it is a good practise to do this. Its not very hard, but there are less possibility to get error.
I mean you should try next query:
insert into SomeTable(Field1, Field2, Field5, Field8) values ($field1Data, $field2Data, $field5Data, $field8Data);
Field3, Field4, Field6, Field7 would be default after this.
I banged my head against the wall for hours trying to understand why I could not complete an OOP insert statement into a MySQL insert table.
In my table I had a column named keys which was not getting inserted into.
I tried a lot of solutions, but then i renamed the column and the error sorted itself out.
Can anybody please tell me why this is occuring?
I am using wampserver 2.4.
It's a reserved word. You have to backtick it if you want to use it:
Like this:
insert into `keys` values (val1, val2) etc...
It's a mysql reserved word. You have to enclose it within ` to use it as column name. But I discourage that, troubles might appear anyway, for example with some libraries.
when your using reserved words you should enclose them inside backtick
for example `keys`
Ok. So right now, my line of code for inserting looks like this:
$query=mysqli_query($con,"insert into orders values (".$user_index.",".$order_date.",".$_POST['item_number'].",".$_POST['price'].",".$_POST['tax'].",'".$_POST['pay_method']."')");
$order_date is a date. $user_index and $_POST['item_number'] are INTs. $_POST['price'] and $_POST['tax'] are doubles. $_POST['pay_method'] is an enum. At the moment, I really couldn't care less about SQL-injection. I just want a working page for now. I'll secure it before it goes live on my website.
My virtual server throws exceptions whether I put in "die(mysqli_error($con))" or not. However, in this case, it is definitely reaching the code that's in the same if statement after the insert statement. Yet, when I run this code, everything looks fine but nothing has changed in the database.
I have tried:
insert into orders (user_index,order_date,item_number,...) values (...)
but it doesn't work that way either.
Finally, yes, I have asked a similar question before. But as I am new to this website, didn't know if there was a way to edit my old question, and figured this would just be easier since I have more details now and I could possibly get the same people as before to answer this with their questions and troubleshooting steps answered; I just went ahead and posted a new question.
You're not adding ' around your string variables. :
If you knew what the values where you'd add them like this:
insert into orders values ('value1','value2' //etc
Your code:
"insert into orders values (".$user_index.".$value2." //etc
Is equivalent to:
"insert into orders values (value1,value2 //etc
Which is wrong, to fix this add in the ' marks like so:
$query=mysqli_query($con,"insert into orders values ('".$user_index."','".$order_date."','".$_POST['item_number']."','".$_POST['price']."','".$_POST['tax']."','".$_POST['pay_method']."')");
For future reference a good way to debug dynamically created SQL statements is to assign them to a variable, named something like $sql, then run your mysqli_query($con,$sql). That way if it's not working you can var_dump or echo your $sql variable and see what's really being queried.
if order_date is a date you need to pad the string with ' on both sides.
$query=mysqli_query($con,"insert into orders values (".$user_index.",'".$order_date."',".$_POST['item_number'].",".$_POST['price'].",".$_POST['tax'].",'".$_POST['pay_method']."')");
I've been inserting entries into a database using PHP for about a year now, then all of a sudden today they stop inserting. I can't work out why. Nothing has changed (as far as I know anyway!) Can anyone tell me what's wrong please:
include('db_functions.php');
connect_to_db();
$query="insert into mixtable (date, djname, title, description, music, tracklist, deleted) values ('".$date."','".$djname."','".$title."','".$description."','".$music."','".$tracklist."', 0)";
$result=mysql_query($query);
if(mysql_affected_rows()==1){
Header("Location:admin.php?op=admin&mix=added&news=null");
}
else{
die("there was a problem");
}
The db_functions work fine, I know this because on another page I call up the database info using the db_functions and it works fine. All of the variables exist as do the table entries. Like I said, this has been working fine for about a year now.
The problem I get is the "there was a problem" error. I've tried showing all errors, but that doesn't show anything either. I can't work out what it is. Any ideas?
Thanks in advance.
try to put "date" in single-quotes because date is a keyword in the current version of MySQL. The error might have happened because of database version upgrade (probably from 4 to 5).
If your table has an auto-increment ID, have you got to the maximum number allowed in that field?
There are few things which you need see.
-> See the below query
"insert into mixtable (date, djname, title, description, music, tracklist, deleted) values ('{$date}','{$djname}','{$title}','{$description}','{$music}','{$tracklist}', 0)";
When you are using double quote you need not use "." to append strings.
-> From the above explanation its quite clear that your query is not executing, try echoing the query and check it in sql editor you should be able to debug it quickly.