Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/113960-mysql-update-row/
Share on other sites

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).

Link to comment
https://forums.phpfreaks.com/topic/113960-mysql-update-row/#findComment-585983
Share on other sites

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?

 

Link to comment
https://forums.phpfreaks.com/topic/113960-mysql-update-row/#findComment-586857
Share on other sites

  • 4 years later...

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. :) 

 

Link to comment
https://forums.phpfreaks.com/topic/113960-mysql-update-row/#findComment-1421367
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/113960-mysql-update-row/#findComment-1421380
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.