User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 456,607 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,492 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 3521 | Replies: 6
Reply
Join Date: Apr 2007
Posts: 7
Reputation: bling_bling_vr6 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
bling_bling_vr6 bling_bling_vr6 is offline Offline
Newbie Poster

Question Tearing my hair out over PHP mysql update script

  #1  
Nov 7th, 2007
Hi,

I have this code. It works fine using PHP 4 and MySQL 5.0. but it doesn't work at all on my webhost (php4 mySQL 4) and I cannot for the life of me figure out why.

Here's my code:

/<?
// Connect database.
include("connectdb.php");

//if "Submit" button is clicked
if($_POST['Submit']){

//Get parameters from form
$id=$_GET['id'];
$name=$_POST['name'];
$email=$_POST['email'];
$tel=$_POST['tel'];

//Update database record
mysql_query("update phonebook set name='$name', email='$email', tel='$tel' where id='$id'");

//Redirect to record view page
header("location:select.php");
exit;
}
// ************* End update part *************


//Get record id to display data for record to be updated
$id=$_GET['id'];

//Put all the results from the query into $result
$result=mysql_query("select * from phonebook where id='$id'");

//Split result into rows and put in $row
$row=mysql_fetch_assoc($result);

?>

<!-- END PHP. This is the HTML form for updating the record -->

<html>
<body>
<!-- set this form to POST method and target itself ($PHP_SELF-->
<form id="form1" name="form1" method="post" action="<? echo $PHP_SELF; ?>">
<p>Name :
<!-- name of this text field is "name" -->
<input name="name" type="text" id="name" value="<? echo $row['name']; ?>"/>
<br />
Email :
<!-- name of this text field is "email" -->
<input name="email" type="text" id="email" value="<? echo $row['email']; ?>"/>
<br />
Tel :
<!-- name of this text field is "tel" -->
<input name="tel" type="text" id="tel" value="<? echo $row['tel']; ?>"/>
</p>
<p>
<input type="submit" name="Submit" value="Submit" />
</p>
</form>
</body>
</html>

---end update.php----

For some reason this fails to update the db, but doesn't give an error. The exact same code works perfectly on my development system (php4 and mysql 5.0).

When I try to do the following in phpmyadmin on my webhost it works fine:

UPDATE phonebook SET name='test', email='test', tel='123' WHERE id='2';

But somehow it doesn't work when i use the update.php script.

The weirdest thing is that I can do a lookup query just fine, for example the code below works just fine and it also uses the same $id variable.

$result=mysql_query("select * from phonebook where id='$id'");
$row=mysql_fetch_assoc($result);

I'm at my wits end and don't know what else to try. Do you have any suggestions?
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Aug 2007
Location: Morrisdale, PA
Posts: 52
Reputation: JeniF is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 5
JeniF's Avatar
JeniF JeniF is offline Offline
Junior Poster in Training

Re: Tearing my hair out over PHP mysql update script

  #2  
Nov 7th, 2007
I don't see anywhere in your form the value of 'id'
Place a hidden field in your form to hold this value so the SQL statement will read the value and update.
I keep hitting "escape", but I'm still here!!!!
:}
Reply With Quote  
Join Date: Apr 2007
Posts: 7
Reputation: bling_bling_vr6 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
bling_bling_vr6 bling_bling_vr6 is offline Offline
Newbie Poster

Re: Tearing my hair out over PHP mysql update script

  #3  
Nov 8th, 2007
Hmm, I tried that. I added a hidden field with the called record with the value of id and then tried to pick it up at the beginning of the if statement instead of $id=$_GET[id] i now have $id = $_POST[record]. But still I don't get my record updated. I even tried to see if my variables even receive the $_POST data and they do...I can echo the data out...just can't update it to the db somehow.
Reply With Quote  
Join Date: Jun 2007
Location: Valley Center, Kansas
Posts: 643
Reputation: kkeith29 is on a distinguished road 
Rep Power: 3
Solved Threads: 72
kkeith29's Avatar
kkeith29 kkeith29 is online now Online
Practically a Master Poster

Re: Tearing my hair out over PHP mysql update script

  #4  
Nov 8th, 2007
try this:

<?php

include("connectdb.php");

if(isset($_POST['Submit'])){

$id = $_REQUEST['id'];
$name = $_REQUEST['name'];
$email = $_REQUEST['email'];
$tel = $_REQUEST['tel'];

$sql = "UPDATE phonebook SET name = '" . $name . "', email = '" . $email . "', tel = '" . $tel . "' WHERE id = '" . $id . "'";
$query = mysql_query($sql);

if ($query) {
header("location:select.php");
exit;
}
}

$id = $_REQUEST['id'];

$result=mysql_query("select * from phonebook where id='$id'");

$row=mysql_fetch_assoc($result);

?>
Last edited by kkeith29 : Nov 8th, 2007 at 10:13 pm.
Reply With Quote  
Join Date: Sep 2007
Location: Buenos Aires
Posts: 30
Reputation: chmazur is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 2
chmazur's Avatar
chmazur chmazur is offline Offline
Light Poster

Re: Tearing my hair out over PHP mysql update script

  #5  
Nov 8th, 2007
Did you check the user/passw/database and privileges on your MySQL 5.0 ?
Reply With Quote  
Join Date: Sep 2007
Location: Budapest
Posts: 252
Reputation: fatihpiristine has a little shameless behaviour in the past 
Rep Power: 0
Solved Threads: 14
fatihpiristine's Avatar
fatihpiristine fatihpiristine is offline Offline
Posting Whiz in Training

Re: Tearing my hair out over PHP mysql update script

  #6  
Nov 9th, 2007
why do you put ' ' to assign numbers to the variables. it usually gives error.

where id=$id")
Reply With Quote  
Join Date: Jun 2007
Location: Valley Center, Kansas
Posts: 643
Reputation: kkeith29 is on a distinguished road 
Rep Power: 3
Solved Threads: 72
kkeith29's Avatar
kkeith29 kkeith29 is online now Online
Practically a Master Poster

Re: Tearing my hair out over PHP mysql update script

  #7  
Nov 9th, 2007
it has never given me an error
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 7:09 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC