Learner Posted November 29, 2013 Report Share Posted November 29, 2013 Hi, I am using MySQL database with PHP. In the following code, I can update and insert data also can delete data. I want to see the update button as UPDATE when I do update operation and when I will insert data the button will be INSERT button, Please help me if you can,,,, thanks in advance. <?php $buttonText = 'Insert'; $con = mysql_connect("localhost","rej","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); $user = new stdClass(); $user->username = null; $user->email = null; $user->website = null; if (!isset($_GET['action'])) $_GET['action'] = null; if ($_GET['action'] == 'edit') { $sqlString = "SELECT * FROM customer WHERE ID =".$_GET['id']."" ; // echo $sqlString; $result = mysql_query($sqlString); $user = mysql_fetch_object($result); // }else if ($_GET['action'] == 'delete'){ if(isset($_GET['id']) and is_numeric($_GET['id'])) { $del= "DELETE FROM customer WHERE id =".$_GET['id']." "; mysql_query($del); echo 'delete customer '. $_GET['id']; } } //print '<pre>'; var_dump($user); print '</pre>'; ?> <html> <body> <form action="webcoach.php" method="post"> username: <input type="text" name="username" value="<?php echo $user->username;?>" /> email: <input type="text" name="email" value= "<?php echo $user->email;?>"/> website: <input type="text" name="website" value = "<?php echo $user->website;?>"/> <input type="hidden" name="id" value="<?php echo $user->ID; ?>" /> <input type="submit" value="Insert" /> <input type= "submit" value = "Update" /> </form> <h3>LIST</h3></br> <?php $result_i = mysql_query("SELECT * FROM customer"); // $mycount= 0 ; while($row = mysql_fetch_array($result_i)) // { $mycount++; echo $mycount. '. <a href="?action=edit&id='.$row['ID'].'">'.$row['username'] . " " . $row['email'] . " " . $row['website'].'</a>'; echo ' <a href="?action=delete&id='.$row['ID'].'">Delete</a>'; echo "<br/>"; } ?> </body> </html> <?php if (count ($_POST)>0 ) { echo "test:".$_POST['id']; if ($_POST['id'] > 0){ // $sqlString = "UPDATE customer SET username = '".$_POST['username']."', email='".$_POST['email']."', website='".$_POST['website']."' WHERE ID= '".$_POST['id']."'"; //echo $sqlString; mysql_query($sqlString); // echo "Updated successful" ; } else { mysql_query("INSERT INTO customer (username, email, website)VALUES ('$_POST[username]','$_POST', '$_POST[website]')"); echo "Inserted successfully "; } mysql_close($con); } ?> Quote Link to comment 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.
Note: Your post will require moderator approval before it will be visible.