Talon21 Posted July 9, 2008 Share Posted July 9, 2008 Hi, I'm using this script to insert my variable into mysql table: <?php include_once ('config.inc.php'); $pageurl = '1'; $mainKeyword = '2'; $title = '3'; $keywords = '4'; $description = '5'; $header = '6'; $site_group ='7'; mysql_select_db("$dbname"); $insert = "INSERT INTO convertech VALUES ('$pageurl','$mainKeyword','$title','$keywords','$description','$header','$site_group','NULL')"; mysql_query($insert); mysql_close(); //include secleted template include('templates/main.php'); ?> It's creating the row and everything is ok the problem is that if I'm updating a variable it won't update the TABLE. How can I solve it? thanks. Quote Link to comment https://forums.phpfreaks.com/topic/113960-mysql-update-row/ Share on other sites More sharing options...
dannyb785 Posted July 9, 2008 Share Posted July 9, 2008 The problem is if you update what variable? And how are you trying to update it now, which is giving you the problem? Quote Link to comment https://forums.phpfreaks.com/topic/113960-mysql-update-row/#findComment-585672 Share on other sites More sharing options...
Talon21 Posted July 9, 2008 Author Share Posted July 9, 2008 The problem occur when I'm updating every variable, I'm not doing something special to update it. I thought it will re-insert it automatically. Quote Link to comment https://forums.phpfreaks.com/topic/113960-mysql-update-row/#findComment-585688 Share on other sites More sharing options...
fenway Posted July 9, 2008 Share Posted July 9, 2008 I see INSERT, not update. Quote Link to comment https://forums.phpfreaks.com/topic/113960-mysql-update-row/#findComment-585743 Share on other sites More sharing options...
Talon21 Posted July 9, 2008 Author Share Posted July 9, 2008 Thank you for the reply, so I can update my code to auto update the rows when a variable is changed Quote Link to comment https://forums.phpfreaks.com/topic/113960-mysql-update-row/#findComment-585772 Share on other sites More sharing options...
dannyb785 Posted July 10, 2008 Share Posted July 10, 2008 When you insert a row, if the database is setup correctly(that is, if you set it up correctly), a unique and incrementing id will be created with each row. When you want to update the contents of the row, you're trying to do insert, with the hopes that it will reinsert the row that you just inserted? Think about it... how will it know which row you want to update? The correct(and only) way to do that is UPDATE and you have to know the id(or any value) of the row you're trying to update. So if you have a user table and you want to update the user with the user_id of 5, you'd do UPDATE User SET user_name='$name', etc etc WHERE user_id='5' doing INSERT will only ever create a new row(unless you're trying to insert a row with an already specified id that you previously deleted, in order to simulate an update, which you should NOT EVER do). Quote Link to comment https://forums.phpfreaks.com/topic/113960-mysql-update-row/#findComment-585983 Share on other sites More sharing options...
Talon21 Posted July 10, 2008 Author Share Posted July 10, 2008 So I can only update the table manually? Quote Link to comment https://forums.phpfreaks.com/topic/113960-mysql-update-row/#findComment-586051 Share on other sites More sharing options...
dannyb785 Posted July 10, 2008 Share Posted July 10, 2008 You can only do anything manually. It never guesses what you want to do. You have to tell it everything Quote Link to comment https://forums.phpfreaks.com/topic/113960-mysql-update-row/#findComment-586060 Share on other sites More sharing options...
Talon21 Posted July 10, 2008 Author Share Posted July 10, 2008 I can use SELECT COUNT(*) FROM to see if some variable has been changed? Quote Link to comment https://forums.phpfreaks.com/topic/113960-mysql-update-row/#findComment-586711 Share on other sites More sharing options...
dannyb785 Posted July 10, 2008 Share Posted July 10, 2008 I can use SELECT COUNT(*) FROM to see if some variable has been changed? Do you have any idea what SELECT COUNT(*) FROM even does? Quote Link to comment https://forums.phpfreaks.com/topic/113960-mysql-update-row/#findComment-586729 Share on other sites More sharing options...
Talon21 Posted July 10, 2008 Author Share Posted July 10, 2008 Yes, see my code: $update_table="SELECT count(*) FROM table WHERE pageurl='$pageurl'"; mysql_query($update_table); if ($update_table > 0){ $update_query="UPDATE convertech SET $main=$main WHERE $main =``"; mysql_query($update_query); } I meant something like that, what do you think? Quote Link to comment https://forums.phpfreaks.com/topic/113960-mysql-update-row/#findComment-586857 Share on other sites More sharing options...
dannyb785 Posted July 11, 2008 Share Posted July 11, 2008 what do you think? I think either a) this is a joke. or b) you need to read about the basics of a php/mysql query. You're missing some key functions in order to interact successfully with a database Quote Link to comment https://forums.phpfreaks.com/topic/113960-mysql-update-row/#findComment-587284 Share on other sites More sharing options...
Talon21 Posted July 11, 2008 Author Share Posted July 11, 2008 This is not the right way to help. Quote Link to comment https://forums.phpfreaks.com/topic/113960-mysql-update-row/#findComment-587372 Share on other sites More sharing options...
fenway Posted July 11, 2008 Share Posted July 11, 2008 This is not the right way to help. Perhaps not... but the sample UPDATE code you provided is very confusing. Quote Link to comment https://forums.phpfreaks.com/topic/113960-mysql-update-row/#findComment-587464 Share on other sites More sharing options...
Annams Posted March 27, 2013 Share Posted March 27, 2013 SEE Sorry to say this, as dannyb said, you must see the basic, even I am new to php and mysql, but i can clarify your doubts, first thing is answer for select count(*) will count the number of rows in the table. and for your question $update_table="SELECT count(*) FROM table WHERE pageurl='$pageurl'"; mysql_query($update_table); ===> Change this to $result=mysql_query($update_table); this will give u number of rows u need where the pageurl value you supplied satisfies. means only onw row that has count of number of rows. Now if ($update_table > 0){ ===> change this as if(mysql_num_fields($result)>0){ (or the value u wish i mean number of rows.)$update_query=mysql_query("UPDATE convertech SET $main=$main WHERE $main =``"); if($update_query) { echo "Trasaction Excecuted Successfully" ; } else { mysql_error(); }} If you still facing any problems dont hesitate to ask me. Quote Link to comment https://forums.phpfreaks.com/topic/113960-mysql-update-row/#findComment-1421367 Share on other sites More sharing options...
DaveyK Posted March 27, 2013 Share Posted March 27, 2013 For one, put your code in code brackets PLEASE. Second, in the first query you SELECT WHERE $pageurl, while the UPDATE uses the $main variable. Since you use different variables, how do you even know the affected row will be the same? Third, you can just run an update query without the COUNT() prior to the query. Even if there is no row to update, it will run without errors. You can then run *_affected_rows() afterwards to see if anything was updated. Quote Link to comment https://forums.phpfreaks.com/topic/113960-mysql-update-row/#findComment-1421380 Share on other sites More sharing options...
requinix Posted March 27, 2013 Share Posted March 27, 2013 If you still facing any problems dont hesitate to ask me. Unlikely since the thread is ALMOST FIVE YEARS OLD. Quote Link to comment https://forums.phpfreaks.com/topic/113960-mysql-update-row/#findComment-1421458 Share on other sites More sharing options...
DaveyK Posted March 27, 2013 Share Posted March 27, 2013 How could I miss that... Quote Link to comment https://forums.phpfreaks.com/topic/113960-mysql-update-row/#findComment-1421495 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.