cloudll Posted December 26, 2014 Share Posted December 26, 2014 I am using php to upload a file to my server, and at the same time inserting the files name and url into my mysql database. $sql = "UPDATE uploads SET name = '$name', url='$target_path'"; $statement = $dbh->prepare($sql); $statement->execute(); This is working, however, when I upload a new file, rather than making a new entry in my database, it just overwrites the first one.I'm quite new at mysql so was wondering how I would make it add new entrys instead of overwriting the current one? Quote Link to comment https://forums.phpfreaks.com/topic/293370-new-mysql-entry-overwriting-existing-entry/ Share on other sites More sharing options...
NotionCommotion Posted December 26, 2014 Share Posted December 26, 2014 What are the columns in the uploads table? Does it have a primary key? Also, you are not doing your prepared statements correct! You do not put the values in the query but add placeholders in the query (either ? or :name), and pass the values as an array to the execute method. Yes, your query will work, but it is not escaping anything. Quote Link to comment https://forums.phpfreaks.com/topic/293370-new-mysql-entry-overwriting-existing-entry/#findComment-1500732 Share on other sites More sharing options...
cloudll Posted December 26, 2014 Author Share Posted December 26, 2014 the columns are id,name and url. ah ok, i will look into prepared statements a little more and try and add an array. Id is the primary key, is that wrong? Quote Link to comment https://forums.phpfreaks.com/topic/293370-new-mysql-entry-overwriting-existing-entry/#findComment-1500734 Share on other sites More sharing options...
NotionCommotion Posted December 26, 2014 Share Posted December 26, 2014 You are updating every record in your table. You need a WHERE clause that limits it to just the applicable PK. Quote Link to comment https://forums.phpfreaks.com/topic/293370-new-mysql-entry-overwriting-existing-entry/#findComment-1500735 Share on other sites More sharing options...
cloudll Posted December 26, 2014 Author Share Posted December 26, 2014 if i use a where clause, does that not need an existing entry? like 'where id = 1' ? Quote Link to comment https://forums.phpfreaks.com/topic/293370-new-mysql-entry-overwriting-existing-entry/#findComment-1500736 Share on other sites More sharing options...
Solution cloudll Posted December 26, 2014 Author Solution Share Posted December 26, 2014 Sorry, figured it out, it was me being extra stupid and and using update instead of insert cheers for your help i will work on the prepared statements some more. Quote Link to comment https://forums.phpfreaks.com/topic/293370-new-mysql-entry-overwriting-existing-entry/#findComment-1500737 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.