Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 08-10-2004, 11:48 AM update record
Junior Talker

Posts: 3
Trades: 0
Hi everybody,
I'm new here. I'm facing a problem bigger than me. I'd like to update/add a record of a mysql table with a php form. Everything work good until I change or insert the id (which is the primary key).
here the code
PHP Code:
<html>

<body>



<?php



$db 
mysql_connect("localhost""root""password");

mysql_select_db("db",$db);



if (
$submit) {

  
// here if no ID then adding else we're editing

  
if ($id) {

    
$sql "UPDATE proprietario SET id='$id',Nome='$Nome',Cognome='$Cognome',Indirizzo='$Indirizzo',Note='$Note' WHERE id=$id";

  } else {

    
$sql "INSERT INTO proprietario (id,Nome,Cognome,Indirizzo,Note) VALUES ('$id','$Nome','$Cognome','$Indirizzo','$Note')";

  }

  
// run SQL against the DB

  
$result mysql_query($sql);

  echo 
"Record updated/edited!<p>";

} elseif (
$delete) {

    
// delete a record

    
$sql "DELETE FROM proprietario WHERE id=$id";    

    
$result mysql_query($sql);

    echo 
"$sql Record deleted!<p>";

} else {

  
// this part happens if we don't press submit

  
if (!$id) {

    
// print the list if there is not editing

    
$result mysql_query("SELECT * FROM proprietario",$db);

    while (
$myrow mysql_fetch_array($result)) {

      
printf("<a href=\"%s?id=%s\">%s %s</a> \n"$PHP_SELF$myrow["id"], $myrow["Nome"], $myrow["Cognome"]);

      
printf("<a href=\"%s?id=%s&delete=yes\">(DELETE)</a><br>"$PHP_SELF$myrow["id"]);

    }

  }



  
?>

  <P>

  <a href="<?php echo $PHP_SELF?>">AGGIUNGI UN RECORD</a>

  <P>

  <form method="post" action="<?php echo $PHP_SELF?>">

  <?php



  
if ($id) {

    
// editing so select a record

    
$sql "SELECT * FROM proprietario WHERE id=$id";

    
$result mysql_query($sql);

    
$myrow mysql_fetch_array($result);

    
$id $myrow["id"];

    
$Nome $myrow["Nome"];

    
$Cognome $myrow["Cognome"];

    
$Indirizzo $myrow["Indirizzo"];

    
$Note $myrow["Note"];

    
// print the id for editing



    
?>
<?php
    
//<input type=hidden name="id" value="<?php echo $id ?>
    

    <?php

  
}



  
?>

  id:<input type="Text" name="id" value="<?php echo $id ?>"><br>

  Nome:<input type="Text" name="Nome" value="<?php echo $Nome ?>"><br>

  Cognome:<input type="Text" name="Cognome" value="<?php echo $Cognome ?>"><br>

  Indirizzo:<input type="Text" name="Indirizzo" value="<?php echo $Indirizzo ?>"><br>

  Note:<input type="Text" name="Note" value="<?php echo $Note ?>"><br>

  <input type="Submit" name="submit" value="Inserisci le informazioni">

  </form>



<?php



}



?>



</body>

</html>
Thank you to all of you that will help me.
moamahi

Last edited by Kyrnt; 08-10-2004 at 01:51 PM..
moamahi is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-10-2004, 11:49 AM oops
Junior Talker

Posts: 3
Trades: 0
the parser did not work...
sorry
moamahi
moamahi is offline
Reply With Quote
View Public Profile
 
Old 08-10-2004, 12:00 PM
zscherween's Avatar
Skilled Talker

Posts: 66
Location: Makati, Philippines
Trades: 0
In your :
$sql = "UPDATE proprietario SET id='$id',Nome='$Nome',Cognome='$Cognome',Indirizzo ='$Indirizzo',Note='$Note' WHERE id=$id";

You can do it this way:

$sql = "UPDATE proprietario SET Nome='$Nome',Cognome='$Cognome',Indirizzo ='$Indirizzo',Note='$Note' WHERE id=$id";

No need to assign for id in update
__________________

Please login or register to view this content. Registration is FREE
- Get your Noynoy shirts here
zscherween is offline
Reply With Quote
View Public Profile Visit zscherween's homepage!
 
Old 08-10-2004, 12:55 PM
Junior Talker

Posts: 3
Trades: 0
Thank you zscherween.
But the fact is that I would like to add record complite of id.
Or i'd like to change it.
Thank you
moamahi
moamahi is offline
Reply With Quote
View Public Profile
 
Old 08-10-2004, 01:01 PM
zscherween's Avatar
Skilled Talker

Posts: 66
Location: Makati, Philippines
Trades: 0
Quote:
Originally Posted by moamahi
Thank you zscherween.
But the fact is that I would like to add record complite of id.
Or i'd like to change it.
Thank you
moamahi
moamahi,
I think there's a logic problem with update statement.
If you want to change ID, you would have to reference the old ID first before changing the other fields.

Hope that helps.
__________________

Please login or register to view this content. Registration is FREE
- Get your Noynoy shirts here
zscherween is offline
Reply With Quote
View Public Profile Visit zscherween's homepage!
 
Reply     « Reply to update record
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.15454 seconds with 12 queries