Deleting Multiple records in php My sql - php

<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="toybox"; // Database name
$tbl_name="Emp"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
// echo $count;
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF"> </td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Delete multiple rows in mysql</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">#</td>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Lastname</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<? echo $rows['EmpId']; ?>"></td>
<td bgcolor="#FFFFFF"><?php echo $rows['EmpId']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['FirstName']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['LastName']; ?></td>
<td bgcolor="#FFFFFF"><?php echo $rows['Email']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
<?php
// Check if delete button active, start this
if($delete){
for($i=0;$i<$count;$i++){
$del_id = $checkbox[$i];
$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
$result = mysql_query($sql);
}
// if successful redirect to delete_multiple.php
if($result){
echo " Record have been deleted";
}
}
mysql_close();
?>
</table>
</form>
</td>
</tr>
</table>

Rather than doing a query for each iteration of your delete-loop, I would build all of the indexes up into a string, and use something like the following:
DELETE FROM tableName
WHERE id IN (1,2,12,53)
Also, your submit button won't come through as $delete, but instead $_POST["delete"]. And with your connection:
mysql_connect("$host", "$username", "$password")
You really ought not use variables like strings (generally) - this should be written as:
mysql_connect($host, $username, $password)
Furthermore, you've got a few more problems in and around your delete-logic. For instance, I pointed out already that your <input type='submit' name='delete' /> button will be known as $_POST["delete"] once it is registered on the server. Likewise, your checkboxes, having an id value of checkbox[] will simply be known as $_POST["checkbox"] on the server.
Also, your $count variable, used in your delete-logic, is based on the earlier query that selected all of the records to show them. It does not reflect the number of checkboxes to be deleted, it reflects the number of records that were shown. As such, your for loop should not be based on it:
for ($i = 0; $i < count($_POST["checkbox"]); $i++)
{
// delete $_POST["checkbox"][$i];
}
And again, I would suggest you build a string of values and run a single query instead of multiple.

Working Code .. Consider Point 1 2 and 3
<?php
// Make a MySQL Connection
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("funconnect") or die(mysql_error());
// Get all the data from the "example" table
$result = mysql_query("SELECT * FROM members")
or die(mysql_error());
$count=mysql_num_rows($result);
echo "<form name='sendmail' method='post' action='memberList.php'><table border='1'>";
echo "<tr> <th>Select</th> <th>Name</th> </tr>";
// keeps getting the next row until there are no more to get
$countSn = 0;
while($row = mysql_fetch_array( $result )) {
// Print out the contents of each row into a table
$chkname = "checkbox".$countSn; #Point 1- Create defferent name for checkboxex like checkbox0, checkbox1
echo "<tr><td><input type='checkbox' name=".$chkname." value=".$row['m_id']." /></td>";
echo "</td><td>";
echo $row['m_name'];
echo "</td></tr>";
$countSn++;
}
echo '<tr><td colspan=2><input name="delete" type="submit" id="delete" value="Delete"></td></tr></table></form>';
$delete=$_POST['delete'];
$checkbox=$_POST['checkbox'];
// Check if delete button active, start this
if($delete){
for($i=0;$i<$count;$i++){
$cname = "checkbox".$i;# Point 2- Create check box name like checkbox0, checkbox1
$checkbox=$_POST[$cname]; #Point 3 - Retrieve data against name
echo $i."===".$checkbox."<br />";
//echo $del_id;
//$sql = "DELETE FROM $tbl_name WHERE id='$del_id'";
//$result = mysql_query($sql);
}
// if successful redirect to delete_multiple.php
if($result){
//echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete_multiple.php\">";
}
}
mysql_close();
?>

Related

mysql / php: Update multiple rows

I try to use the below code to update multiple rows, the below code can view the results of rows but it can not be updated, where is wrong place? How to modify it ?
<?php
$host="localhost"; // Host name
$username="abc"; // Mysql username
$password="abc123"; // Mysql password
$db_name="abc"; // Database name
$tbl_name="BRAddress"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("Cannot connect");
mysql_select_db("$db_name")or die("Cannot select Database");
$sql="SELECT * FROM $tbl_name WHERE br_no='62779457'";
$result=mysql_query($sql);
// Count table rows
$count=mysql_num_rows($result);
?>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="post" action="">
<tr>
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td align="center"><strong>BR No.</strong></td>
<td align="center"><strong>Date of Register</strong></td>
<td align="center"><strong>Address</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center">
<? $br_no[]=$rows['br_no']; ?><? echo $rows['br_no']; ?>
</td>
<td align="center">
<input name="br_date_of_register[]" type="date" id="br_date_of_register" value="<? echo $rows['br_date_of_register']; ?>">
</td>
<td align="center">
<input name="br_address[]" type="text" size="60" id="br_address" value="<? echo $rows['br_address']; ?>">
</td>
</tr>
<?php
}
?>
<tr>
<td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
// Check if button name "Submit" is active, do this
if($Submit){
for($i=0;$i<$count;$i++){
$sql1="UPDATE $tbl_name SET
br_date_of_register='$br_date_of_register[$i]',
br_address='$br_address[$i]'
WHERE br_no='$br_no[$i]'";
$result1=mysql_query($sql1);
}
}
if($result1){
header("location:update_sample.php");
}
mysql_close();
?>
Thank you very much for your help & support !
I think that you need to change this part
if($Submit){
to
if($_POST('Submit')){
I haven't run the whole or looked at entire code, but you have nothing that defines $Submit variable though from what I see.
Or you can put in
$Submit = $_POST('Submit');
before the if statement.
Let me know how you go.
Cheers
Your POST variable are empty. $br_date_of_register has no value. You must use this like following
$br_date_of_register = $_POST[br_date_of_register];
$br_address = $_POST[br_address];
for($i=0;$i<$count;$i++){
$sql1="UPDATE $tbl_name SET
br_date_of_register='$br_date_of_register[$i]',
br_address='$br_address[$i]'
WHERE br_no='$br_no[$i]'";
$result1=mysql_query($sql1);
}
Edit
if($Submit)
To
if($_SERVER['REQUEST_METHOD'] == "POST")
Considering your Submit check,
you can use this,
if(isset($_POST["Submit"]))
{
}
Further in your SQL statement, do this,
$sql1='UPDATE ' . $tbl_name . ' SET
br_date_of_register = ' . $br_date_of_register[$i] .
' , br_address = ' . $br_address[$i] .
' WHERE br_no = ' . $br_no[$i];

Repeating on adding data in Database

I have a problem in my guestbook in php. After I fill up the guestbook, it works fine but when I look to my database, all the data I entered is repeated more than once.
guestbook.php
<table border="0" width="920" bgcolor="#1d1c1b" id="round"><tr><td>
<table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
<tr><br>
<td><strong><h2>SEACO's Guestbook</h2> </strong></td>
</tr>
</table>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form id="form1" name="form1" method="post" action="addguestbook.php">
<td>
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td width="117">Name</td>
<td width="14">:</td>
<td width="357"><input name="name" type="text" id="name" size="40" placeholder="Enter your name here" required/></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><input name="email" type="text" id="email" size="40" placeholder="Enter your email here (Optional)"/></td>
</tr>
<tr>
<td valign="top">Comment</td>
<td valign="top">:</td>
<td><textarea name="comment" cols="40" rows="3" id="comment" placeholder="Your comment here" required></textarea></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Submit" class="button"/> <input type="reset" name="Submit2" value="Reset" class="button" /></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
</table><br><center><strong><button class="button">View Guestbook</button> </strong></center>
<br><br></table>
addguestbook.php
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="chess"; // Database name
$tbl_name="guestbook"; // Table name
$name = $_POST['name'];
$email = $_POST['email'];
$comment = $_POST['comment'];
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
mysql_select_db("$db_name")or die("cannot select DB");
$datetime=date("y-m-d h:i:s"); //date time
$sql="INSERT into $tbl_name(name, email, comment, datetime)values('$name', '$email', '$comment', '$datetime')";
$result=mysql_query($sql);
//check if query successful
if($result){
echo "<br><br><center><font color='white' size='5'>Successful</font> <img src='images/cmark.png' width='40px'></center>";
echo "<BR>";
// link to view guestbook page
echo "<center><a href='viewguestbook.php'><button class='button'>View guestbook</button></a></center>";
echo '<br><br>';
}
else {
echo "ERROR";
}
mysql_close();
?>
viewguestbook.php
<table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
<tr>
<td><strong><font color="white">View Guestbook</font> | <button class="button">Sign Guestbook</button> </strong></td>
</tr>
</table>
<br>
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="chess"; // Database name
$tbl_name="guestbook"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
mysql_select_db("$db_name")or die("cannot select DB");
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
while($rows=mysql_fetch_array($result)){
?>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td><table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td width="117">Name</td>
<td width="14">:</td>
<td width="357" id="input"><?php echo $rows['name']; ?></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td id="input"><?php echo $rows['email']; ?></td>
</tr>
<tr>
<td valign="top">Comment</td>
<td valign="top">:</td>
<td id="input"><?php echo $rows['comment']; ?></td>
</tr>
<tr>
<td valign="top">Date/Time </td>
<td valign="top">:</td>
<td id="input"><?php echo $rows['datetime']; ?></td>
</tr>
</table></td>
</tr>
</table>
<?php
}
mysql_close(); //close database
?>
</table>
<?php
if (!isset($_POST['name'])) {
header ("Location: guestbook.php");
exit();
}
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="chess"; // Database name
$tbl_name="guestbook"; // Table name
$name = $_POST['name'];
$email = $_POST['email'];
$comment = $_POST['comment'];
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
mysql_select_db("$db_name")or die("cannot select DB");
$datetime=date("y-m-d h:i:s"); //date time
$sql="INSERT into $tbl_name(name, email, comment, datetime)values('$name', '$email', '$comment', '$datetime')";
$result=mysql_query($sql);
unset($_POST['name']);
//check if query successful
if($result){
echo "<br><br><center><font color='white' size='5'>Successful</font> <img src='images/cmark.png' width='40px'></center>";
echo "<BR>";
// link to view guestbook page
echo "<center><a href='viewguestbook.php'><button class='button'>View guestbook</button></a></center>";
echo '<br><br>';
}
else {
echo "ERROR";
}
mysql_close();
?>
There is no way to execute your query 6 times until you resubmit the form by reloading or any ajax request. If your query is executed then it should return true in $result. you can use exit() inside condition to check if it works and Try to check if form submitted by submit button like this:
<?php
if(isset($_POST['Submit'])){
$host="localhost"; // Host name
$username="root"; // Mysql username
$password="root"; // Mysql password
$db_name="test"; // Database name
$tbl_name="users"; // Table name
$name = $_POST['name'];
$email = $_POST['email'];
$comment = $_POST['comment'];
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect server ");
mysql_select_db("$db_name")or die("cannot select DB");
$datetime=date("y-m-d h:i:s"); //date time
$sql="INSERT into $tbl_name(name, email, comment, datetime) values ('$name', '$email', '$comment', '$datetime')";
$result=mysql_query($sql);
//check if query successful
if($result){
echo "<br><br><center><font color='white' size='5'>Successful</font> <img src='images/cmark.png' width='40px'></center>";
echo "<BR>";
// link to view guestbook page
echo "<center><a href='viewguestbook.php'><button class='button'>View guestbook</button></a></center>";
echo '<br><br>';
exit();
}
else {
echo "ERROR";
}
mysql_close();
}
else{
echo "not set";
}

Inserting multiple rows into a MySQL database from a HTML Form

I want to pull 2 fields from a MySQL database and them use them in 2 hidden form fields along with allowing the user to input data into additional form fields. I am using the following code to do so:
$yesterday = date('d/m/Y',strtotime("-1 days"));
$sql="SELECT acct_backup_servers.server_desc, acct_customer.customer from acct_backup_servers, acct_customer
where acct_backup_servers.cust_act=acct_customer.acct_no ";
$result=mysql_query($sql);
echo '<form action="http://www.bimotech.co.uk/admin/insert_backup_rec/index.php" method="post">
<table>
<tr>
<th width="20%" align="left" valign="top">Server</th>
<th width="20%" align="left" valign="top">Customer</th>
<th align="left" valign="top">Date</th>
<th width="25%" align="left" valign="top">Status</th>
<th width="30%" align="left" valign="top">Notes</th>
<th align="left" valign="top"></th>
</tr>';
while($rows=mysql_fetch_array($result)){
$server_notes = $rows['server_desc'];
echo "
<input type='hidden' name='server' value='".$rows['server_desc']."'>
<input type='hidden' name='customer' value='".$rows['customer']."'>
<tr>
<td valign='top'>".$rows['server_desc']."</td>
<td valign='top'>".$rows['customer']."</td>
<td valign='top'><input type='text' name='date' size='10' id='datepicker' value='".$yesterday."'></td>
<td valign='top'>
<select name='status'>";
$sql2 = mysql_query("SELECT cat_desc FROM acct_backup_category");
while ($row = mysql_fetch_array($sql2)){
echo "<option value='category'>" . $row['cat_desc'] . "</option>";
}
echo "</select></td>
<td valign='top'><textarea name='notes'></textarea></td>
<td valign='top'><input type='checkbox' name='completed'></td>
</tr>";
}
echo "</table>
</br>
<input type='submit' class='btn' value='Submit'>
</form>";
mysql_close();
I have setup the page that this form post to and echo'd the results but all I am receiving is the last record. Is there a way that I can get all the results for each row and post them in one go to the database?
EDIT: PHP Echo below.
$backup_date = $_POST['date'];
$server_desc = $_POST['server'];
$customer = $_POST['customer'];
echo $backup_date;
echo $server_desc;
echo $customer;
I am very much a PHP newbie so appreciate anyones help on this.
Further EDIT:
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
foreach($customer as $key => $value) {
$date = $backup_date[$key];
$server = $server_desc[$key];
$customer_id = $value;
$backup_code = $status[$key];
$notes_field = $notes[$key];
$been_checked = $checked[$key];
mysql_query("INSERT INTO acct_backups (backup_date, server_code, cust_act, backup_code, notes, checked) VALUES ($date, $server, $customer_id, $backup_code, $notes_field, $been_checked)");
}
mysql_close();
Thanks,
John
Make hidden input as an array.
Like,
<input type='hidden' name='server[]' value='".$rows['server_desc']."'>
<input type='hidden' name='customer[]' value='".$rows['customer']."'>
Then print,
print_r($backup_date);
print_r($server_desc);
print_r($customer);
Sample Code:
/* MySQL Conncection goes here*/
foreach($customer as $key => $value) {
$customer_id = $value;
$date = $backup_date[$key];
$server = $server_desc[$key];
mysql_query("INSERT INTO table_name VALUES($customer_id, $date, $server)");
}

PHP to delete SQL row with multiple checkboxes

I am trying to delete multiple rows with chekboxes. Below is my code
<?php
$host="localhost"; // Host name
$username="****"; // Mysql username
$password="****"; // Mysql password
$db_name="****"; // Database name
$tbl_name="****"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$result = mysql_query("SELECT * FROM members WHERE dealer='Panzer Protection'");
?>
<form name="form1" method="post" action="">
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td bgcolor="#666666"><input name="checkbox[]" type="checkbox" id="checkbox[]"
value="<? echo $rows['member_id']; ?>"></td>
<td bgcolor="#666666"><? echo $rows['member_id']; ?></td>
<td bgcolor="#666666"><center>
<? echo $rows['member_msisdn']; ?></td>
<td bgcolor="#666666"><center>
<? echo $rows['member_name']; ?></td>
<td bgcolor="#666666"><div align="center"><? echo $rows['dealer']; ?></div>
</td>
<td align="center" bgcolor="#FFFFFF"><a href="control_clientinfo.php?member_id=
<? echo $rows['member_id']; ?>" class="update">Look Up</a></td>
</tr>
<?php
}
?>
<tr>
<td colspan="6" align="center" bgcolor="#FFFFFF"><input name="delete" type="submit"
id="delete" value="Delete"></td>
</tr>
</form> //Forgot form close in past
<?php
// Check if delete button active, start this
if($_POST['delete']){
for($i=0;$i<$count;$i++){
$i = 0;
while(list($key, $val) = each($_POST['checkbox'])) {
$sql = "DELETE FROM $tbl_name WHERE id='$val'";
mysql_query($sql);
$i += mysql_affected_rows();
}
}
// if successful redirect to
if($result){
echo "<meta http-equiv=\"refresh\" content=\"0;URL=control_clientlistdel.php\">";
}
}
mysql_close();
?>
It shows me the list i call and i can tick the boxes. If i hit delete button it just refreshes the screen and the one i ticked is still there
First things first.
It's bad idea to use mysql as it is really old and it's deprecated.
Second, where do you assign your variables ($delete, $count)
you have to check if the delete key of your POST is set:
if (isset($_POST['delete'])) { // Then the form has been submitted
after this, assign your $count variable
$checkbox = $_POST['checkbox'];
$count = count($checkbox);
And everything must work fine.
Final result
if (isset($_POST['delete'])) {
$checkbox = $_POST['checkbox'];
$count = count($checkbox);
for($i = 0; $i < $count; $i++) {
$id = (int) $checkbox[$i]; // Parse your value to integer
if ($id > 0) { // and check if it's bigger then 0
mysql_query("DELETE FROM table WHERE member_id = $id");
}
}
}
Check out the mysqli and the PDO drivers for interacting with the database.
not sure if its a typo or not.. but you you have a missing from end tags and <table> in the posted code..
....
<td colspan="6" align="center" bgcolor="#FFFFFF">
<input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>
</form> //here
and you need to check the posted $delete value in if condition..the correct way is to use $_POST since you are using method as pos.. here method="post".
updated
if(isset($_POST) && $_POST['delete']){ //here
$count=count($_POST['checkbox']);
for($i=0;$i<$count;$i++){
$sql = "DELETE FROM $tbl_name WHERE id='".$_POST['checkbox'][$i]."'";
mysql_query($sql);
}
}
you can use header() to redirect in php
header( 'Location: http://www.yoursite.com/ontrol_clientlistdel.php' ) ;

PHP/MySQL allowing current user to edit their account information

I have created 2 pages
update.php
edit.php
We start on edit.php so here is edit.php's script
<?php
session_start();
$id = $_SESSION["id"];
$username = $_POST["username"];
$fname = $_POST["fname"];
$password = $_POST["password"];
$email = $_POST["email"];
mysql_connect('mysql13.000webhost.com', 'a2670376_Users', 'Password') or die(mysql_error());
echo "MySQL Connection Established! <br>";
mysql_select_db("a2670376_Pass") or die(mysql_error());
echo "Database Found! <br>";
$query = "UPDATE members SET username = '$username', fname = '$fname',
password = '$password' WHERE id = '$id'";
$res = mysql_query($query);
if ($res)
echo "<p>Record Updated<p>";
else
echo "Problem updating record. MySQL Error: " . mysql_error();
?>
<form action="update.php" method="post">
<input type="hidden" name="id" value="<?=$id;?>">
ScreenName:<br> <input type='text' name='username' id='username' maxlength='25' style='width:247px' name="username" value="<?=$username;?>"/><br>
FullName:<br> <input type='text' name='fname' id='fname' maxlength='20' style='width:248px' name="ud_img" value="<?=$fname;?>"/><br>
Email:<br> <input type='text' name='email' id='email' maxlength='50' style='width:250px' name="ud_img" value="<?=$email;?>"/><br>
Password:<br> <input type='text' name='password' id='password' maxlength='25' style='width:251px' value="<?=$password;?>"/><br>
<input type="Submit">
</form>
Now here is the update.php page where I am having the major problem
<?php
session_start();
mysql_connect('mysql13.000webhost.com', 'a2670376_Users', 'Password') or die(mysql_error());
mysql_select_db("a2670376_Pass") or die(mysql_error());
$id = (int)$_SESSION["id"];
$username = mysql_real_escape_string($_POST["username"]);
$fname = mysql_real_escape_string($_POST["fname"]);
$email = mysql_real_escape_string($_POST["email"]);
$password = mysql_real_escape_string($_POST["password"]);
$query="UPDATE members
SET username = '$username', fname = '$fname', email = '$email', password = '$password'
WHERE id='$id'";
mysql_query($query)or die(mysql_error());
if(mysql_affected_rows()>=1){
echo "<p>($id) Record Updated<p>";
}else{
echo "<p>($id) Not Updated<p>";
}
?>
Now on edit.php I fill out the form to edit the account "test" while I am logged into it now once the form if filled out I click on Submit button
and it takes me to update.php and it returns this
(0) Not Updated
(0) <= id of user logged in
Not Updated <= MySql Error from
mysql_query($query)or die(mysql_error());
if(mysql_affected_rows()>=1){
I want it to update the user logged in and if I am not mistaken in this script it says
$id = (int)$_SESSION["id"];
which updates the user with the id of the person who is logged in
but it isn't updating, its saying that no tables were effected
if it helps here's my MySQL Database picture
just click here http://i50.tinypic.com/21juqfq.png
if this could possibly be any help to find the solution I have 2 more files delete.php and delete_ac.php they have can remove users from my sql database and they show the user id and it works there are no bugs in this script at all PLEASE DO NOT MAKE SUGGESTIONS FOR THE SCRIPTS BELOW
delete.php first
<?php
$host="mysql13.000webhost.com"; // Host name
$username="a2670376_Users"; // Mysql username
$password="PASSWORD"; // Mysql password
$db_name="a2670376_Pass"; // Database name
$tbl_name="members"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// select record from mysql
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
?>
<table border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td colspan="8" style="bgcolor: #FFFFFF"><strong><img src="http://i47.tinypic.com/u6ihk.png" height="30" widht="30">Delete data in mysql</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF"><strong>Id</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>UserName</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>FullName</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Password</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Email</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Date</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Ip</strong></td>
<td align="center" bgcolor="#FFFFFF"> </td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td bgcolor="#FFFFFF"><? echo $rows['id']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['username']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['fname']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['password']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['email']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['date']; ?></td>
<td bgcolor="#FFFFFF"><? echo $rows['ip']; ?></td>
<td bgcolor="#FFFFFF">delete</td>
</tr>
<?php
// close while loop
}
?>
</table>
<?php
// close connection;
sql_close();
?>
and now delete_ac.php
<table width="500" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td colspan="8" bgcolor="#FFFFFF"><strong><img src="http://t2.gstatic.com/images? q=tbn:ANd9GcS_kwpNSSt3UuBHxq5zhkJQAlPnaXyePaw07R652f4StmvIQAAf6g" height="30" widht="30">Removal Of Account</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">
<?php
$host="mysql13.000webhost.com"; // Host name
$username="a2670376_Users"; // Mysql username
$password="javascript00"; // Mysql password
$db_name="a2670376_Pass"; // Database name
$tbl_name="members"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// get value of id that sent from address bar
$id=$_GET['id'];
// Delete data in mysql from row that has this id
$sql="DELETE FROM $tbl_name WHERE id='$id'";
$result=mysql_query($sql);
// if successfully deleted
if($result){
echo "Deleted Successfully";
echo "<BR>";
echo "<a href='delete.php'>Back to main page</a>";
}
else {
echo "ERROR";
}
?>
<?php
// close connection
mysql_close();
?>
</td>
</tr>
</table>
Try below query, and post output here. Also execute same echo query in phpmyadmin to see what happend.
echo $query="UPDATE members
SET username = '$username', fname = '$fname', email = '$email', password = '$password'
WHERE id=$id";
From your link it seems anyone can directly go to edit page, that is wrong.
You need to add condition that if user is login then only he can update his profile.
Could you check on edit.php if $id is actually set to some value instead of empty? Might be that the id is never stored in the session
Right now your $id is null. (int)$id is 0.
So when you try to update WHERE id=$id you are basically saying WHERE id=0
If id is an Auto Increment Integer then you are not going to have an id=0 and nothing will be updated. You need to create the $_SESSION['id'] by putting something in it.
$_SESSION['id'] = XXXX;
$sqlshow =# mysqli_query($con,"SELECT `id`, `Registration_No`, `First_Name`, `Middle_Name`, `Sir_Name`, `Sex`, `Birth_Day`, `Email`, `Address`, `Phone` FROM `cdtistudent` WHERE id=40"); while($row = #mysqli_fetch_object($sqlshow)) {
update.php page
if(isset($_POST["update"])){
$Registration = $_POST['Registration'];
$First_Name = $_POST['First'];
$Middle_Name = $_POST['Middle'];
$Sir_Name = $_POST['Sir'];
$Sex = $_POST['Sex'];
$Birth_Day = $_POST['Birth'];
$Email = $_POST['Email'];
$Address = $_POST['Address'];
$Phone=$_POST['Phone'];
$id=$_POST['id'];
$sqlupdate =mysqli_query($con,"UPDATE cdtistudent
SET
Registration_No='$Registration',
First_Name='$First_Name',
Middle_Name='$Middle_Name',
Sir_Name='$Sir_Name',
Sex='$Sex',
Birth_Day='$Birth_Day',
Email='$Email',
Address='$Address',
Phone='$Phone'
WHERE id='$id'");
if($sqlupdate === false){
die("".mysqli_error($con));
}}
it look like
UPDATE cdtistudent SET id=[value-1],Registration_No=[value-2],First_Name=[value-3],Middle_Name=[value-4],Sir_Name=[value-5],Sex=[value-6],Birth_Day=[value-7],Email=[value-8],Address=[value-9],Phone=[value-10] WHERE id=?;
and edit.php page
$sqlshow =# mysqli_query($con,"SELECT `id`, `Registration_No`, `First_Name`, `Middle_Name`, `Sir_Name`, `Sex`, `Birth_Day`, `Email`, `Address`, `Phone` FROM `cdtistudent`
WHERE id=40");
while($row = #mysqli_fetch_object($sqlshow))
{

Categories