Hello I can't get my DB to update. If I run it in mySQL querybox the query works but not in my php code...
I don't get any errors but it just will not update.
Can someone help me?
php
if(isset($_POST['btnSignup']))
{
$con=mysqli_connect('test', 'test', 'test', 'test');
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if (isset($name)) {
mysqli_query($con,
"UPDATE `tbl_klant` \n"
. " SET `kl_aanspreek`= \'$aansp\',\n"
. " `kl_voornaam`= \'$voornaam\',\n"
. " `kl_achternaam`= \'$achternaam\',\n"
. " `kl_email`= \'$email\',\n"
. " `kl_gsm`= $gsm ,\n"
. " `kl_fax`= $fax ,\n"
. " `kl_telefoon`= $telefoon,\n"
. " `kl_straat`= \'$straat\',\n"
. " `kl_postcode`= $post,\n"
. " `kl_gemeente`= \'$gemeente\',\n"
. " `kl_huisnr`= $huisnr,\n"
. " `kl_firmanaam`= \'$firma\',\n"
. " `kl_btwnr`= \'$btwnr\'\n"
. "WHERE `klant_id`= $name");
}else {
echo "werkt niet";
}
echo "voorbij query";
mysqli_close($con);
}
html
<form name="regForm" method="post" action="<?php $_PHP_SELF ?>" onsubmit="return(validateRegistreer());">
<table width="400" border="0" cellspacing="1" cellpadding="2">
<tr>
<td >Aanspreek titel</td>
<td><select name="kl_aanspreek" id="kl_aanspreek">
<option value="Dhr.">Dhr.</option>
<option value="Mevr.">Mevr.</option>
</select>
</td>
</tr>
<tr>
<td >Voornaam *</td>
<td><input name="kl_voornaam" type="text" id="kl_voornaam" required value="<?php echo $voornaam ?>"></td>
</tr>
<tr>
<td >Achternaam *</td>
<td><input name="kl_achternaam" type="text" id="kl_achternaam" required value="<?php echo $achternaam ?>"></td>
</tr>
the code of the table repeat itself
<tr>
<td width="100"> </td>
<td>
<input name="btnSignup" type="submit" id="btnSignup" value="Wijzig" >
</td>
</tr>
</table>
try next
$query = "UPDATE tbl_klant SET kl_aanspreek= '$aansp',
kl_voornaam= '$voornaam'
kl_achternaam= '$achternaam',
kl_email= '$email',
kl_gsm= '$gsm' ,
kl_fax= '$fax' ,
kl_telefoon= '$telefoon',
kl_straat= '$straat',
kl_postcode= '$post',
kl_gemeente= '$gemeente',
kl_huisnr= '$huisnr',
kl_firmanaam= '$firma',
kl_btwnr= '$btwnr'
WHERE klant_id= '$name'";
mysqli_query($con,$query);
Try :
if(isset($_POST['btnSignup']))
{
$con=mysqli_connect('test', 'test', 'test', 'test');
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if (isset($name)) {
if(!mysqli_query($con,
"UPDATE `tbl_klant` "
. " SET `kl_aanspreek`= '$aansp',"
. " `kl_voornaam`= '$voornaam',"
. " `kl_achternaam`= '$achternaam',"
. " `kl_email`= '$email',"
. " `kl_gsm`= $gsm ,"
. " `kl_fax`= $fax ,"
. " `kl_telefoon`= $telefoon,"
. " `kl_straat`= '$straat',"
. " `kl_postcode`= $post,"
. " `kl_gemeente`= '$gemeente',"
. " `kl_huisnr`= $huisnr,"
. " `kl_firmanaam`= '$firma',"
. " `kl_btwnr`= '$btwnr'"
. " WHERE `klant_id`= $name")){
echo("Error description: " . mysqli_error($con));
}
}else {
echo "werkt niet";
}
echo "voorbij query";
mysqli_close($con);
}
Related
I am having an issue deleting the row from the table. When I click on the 'delete' button it does take me to the next page and it says 'Removed 0 rows from player'. Basically, it is executing correctly, but I am unable to delete the selected row. I have been able to display and add to the table.
Player.php
<table id="table table-bordered">
<tr>
<th>Id#</th>
<th>Player(s)</th>
<th>Position</th>
</tr>
if(!($stmt = $mysqli->prepare("SELECT id_Player, name_Player, position_Player FROM player s ORDER BY position_Player ASC"))){
echo "Prepare failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
if(!$stmt->execute())
{
echo "Execute failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
if(!$stmt->bind_result($id_Player, $name_Player, $position_Player))
{
echo "Bind failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
while($stmt->fetch()){
echo "<tr><td> $id_Player </td> <td> $name_Player </td><td> $position_Player </td>";
?>
<td>
<form id="delete" method="post" action="deletePlayers.php">
<input type="submit" name="id_Player" value="Delete!"/>
</form>
</td>
</tr>
deletePlayers.php
if(!($stmt = $mysqli->prepare("DELETE FROM player WHERE id_Player = ?"))){
echo "Prepare failed: " . $stmt->errno . " " . $stmt->error;}
if(!($stmt->bind_param("s",$_POST['id_Player']))){
echo "Bind failed: " . $stmt->errno . " " . $stmt->error;}
if(!$stmt->execute()){
echo "Execute failed: " . $stmt->errno . " " . $stmt->error;}
else {
echo "Removed " . $stmt->affected_rows . " row from player. <br/><br/><strong> Returning to 'Add Players'</strong>";}
The problem lies in this line of code,
if(!($stmt->bind_param("s",$_POST['id_Player']))){}
Here the value of $POST['id_player'] is Delete! because you are passing the name of the input type submit in your HTML code and I think that you don't have any id which equals to Delete! in your database.
The Solution
What you need to do is that you need to use a hidden input which will hold the id_Player value like this,
if(!($stmt = $mysqli->prepare("SELECT id_Player, name_Player, position_Player FROM player s ORDER BY position_Player ASC"))){
echo "Prepare failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
if(!$stmt->execute())
{
echo "Execute failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
if(!$stmt->bind_result($id_Player, $name_Player, $position_Player))
{
echo "Bind failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
while($stmt->fetch()){
echo "<tr><td> $id_Player </td> <td> $name_Player </td><td> $position_Player </td>";
?>
<td>
<form id="delete" method="post" action="deletePlayers.php">
<input type="hidden" name="id_Player" value="<?= $id_Player ?>"/>
<input type="submit" value="Delete!"/>
</form>
</td>
</tr>
The problem may be here in the html form, but this is linked correctly
<html>
<head>
</head>
<body>
<form method="post" action="mod2_findrecord.php">
<table width="400" border="0" cellspacing="1" cellpadding="2">
<tr>
<td width="100">Last Name</td>
<td><input name="last_name" type="text" id = "last_name"></td>
</tr>
<tr>
<td width="100"> </td>
<td>
<input name="find" type="submit" id="add" value="Find Record">
</td>
</tr>
</table>
</form>
</body>
</html>
<?php
include 'mod2_opendb.php';
include 'mod2_config.php';
$lastname = (isset($_POST['last_name']) ? $_POST['last_name'] : '');
$sql= " SELECT employeeid, managementid, first_name, last_name, age, employment_period FROM employees WHERE last_name = '$lastname' ";
This SQL statement works in my database
$result = mysqli_query($con, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "<b>Record found</b><br>";
echo "<b>EmployeeID: " . $row["employeeid"]. "</b><br>";
echo "<b>ManagementID: " . $row["managementid"]. "</b><br>";
echo "<b>Name: " . $row["first_name"]. " " . $row["last_name"]. "</b><br>";
echo "<b>Age: " . $row["age"]. "</b><br>";
echo "<b>Employment Period In Months: " . $row["employment_period"]. "</b><br>";
}
} else {
echo "Sorry there are no matches! Please check your entry and try again.";
}
This is the message that keeps displaying, it does not allow me to pull information
mysqli_close($con);
?>
This question already has answers here:
undefined offset PHP error
(4 answers)
Closed 9 years ago.
I am trying to get a basic edit function working and have come up with the following.
One the first page I have:
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['product_name'] . "</td>";
echo "<td>" . $row['product_price'] . "</td>";
echo"<td>Delete";
echo"<td>Edit";
echo "</tr>";
}
echo "</table>";
This all work correctly apart from when I try and get the id for Edit on the next page.
The next page:
<?php
if(isset($_POST['edit']))
{
$con=mysqli_connect("localhost","root","","db_test");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if (isset ($_GET['id'])) { $product_id = strip_tags($_GET['id']); }
$productname =strip_tags($_POST['nameAdd']);
$productdesc =strip_tags($_POST['descAdd']);
$productimg =strip_tags($_POST['imageAdd']);
$price =strip_tags($_POST['priceAdd']);
$sql = "UPDATE tbl_products SET product_name ='$productname', product_description ='$productdesc',
product_img ='$productimg',product_price ='$price' WHERE product_id = '$product_id'";
if (!$insert = $con->prepare($sql))
die('Query failed: (' . $con->errno . ') ' . $con->error);
if (!$insert->bind_param('ssss', $productname, $productdesc, $productimg, $price ))
die('Binding parameters failed: (' . $insert->errno . ') ' . $insert->error);
if (!$insert->execute())
die('Execute failed: (' . $insert->errno . ') ' . $insert->error);
else
echo "Edit Successful!";
mysqli_close($con);
echo(" </div>
<div id='right'><br><br><img src='Red Carpet Theatre Company/images/MayDayGroup.jpg' width='350' height='250' alt='Group Photo'/>
<p> </p>
</div>
<div id='footer'>");
}
else
{
echo("
<FORM action='".$_SERVER['PHP_SELF']."' METHOD=post>
<input type='hidden' name='edit' value='edit'>
<table border='3'>
<tr>
<td> Product Name :</td><td><input name=nameAdd type ='text' size'14'> </input></td>
</tr>
<tr>
<td> Product Description :</td><td><input name='descAdd' type ='text' size'14'> </input></td>
</tr>
<tr>
<td> Product Image URL :</td><td><input name='imageAdd' type ='text' size'14'> </input></td>
</tr>
<tr>
<td> Product Price :</td><td><input name='priceAdd' type ='text' size'14'> </input></td>
</tr>
<tr>
<td><input type='submit' name='Submit' value='Submit'></td>
</tr>
</table>
</FORM>
</div>
<div id='right'><br><br><img src='Red Carpet Theatre Company/images/MayDayGroup.jpg' width='350' height='250' alt='Group Photo'/>
<p> </p>
</div>
<div id='footer'>
");
}
?>
For some reason I keep getting undefined errors for the product_id variable. Any ideas why? It should be getting it from the previous page using "isset ($_GET['id'])) Thank you.
$_GET['id'] isn't defined because your form method is POST. How are you getting the product ID? Is it in the url structure?
as you are using this using MYSQLI_ and Not PDO
you should not use query like:
if (!$insert = $con->prepare($sql))
die('Query failed: (' . $con->errno . ') ' . $con->error);
if (!$insert->bind_param('ssss', $productname, $productdesc, $productimg, $price ))
die('Binding parameters failed: (' . $insert->errno . ') ' . $insert->error);
if (!$insert->execute())
die('Execute failed: (' . $insert->errno . ') ' . $insert->error);
else
echo "Edit Successful!";
instead this u shall use it like this:
if (!$insert = mysqli_prepare($con,$sql))
die('Query failed: (' . mysqli_errno($con) . ') ' . mysqli_error();
if (!mysqli_stmt_bind_param($insert,'ssss', $productname, $productdesc, $productimg, $price ))
die('Binding parameters failed: (' . mysqli_errno($insert). ') ' . mysqli_error($insert);
if (!mysqli_stmt_execute($insert))
die('Execute failed: (' . mysqli_errno($insert) . ') ' . mysqli_error($insert);
else
echo "Edit Successful!";
Now you code will work fine (Its just mysqli_* way of doing it
The href syntax looks off. What does the link look like in firebug.
I'm very new to PHP and SQL. For a school assignment, I need to create a form for users to update customer data. However, I notice that the update function only updates the customer with the last ID in the data set. For example, if I have 4 customers in the data set. I used a drop down box t list the customer id. When I select id, 1 ~ 3, it says that 0 row is updated. It only works when I select id, 4. So I can only update the 4th one. Can someone take a look at my code and give me some tips on what the issue is and how to fix it? Thank you!
Here is the "updatecustomer.php" code:
<?php
//Turn on error reporting
ini_set('display_errors', 'On');
//Connects to the database
$mysqli = new mysql(SERVER_NAME, USERNAME,PASSWORD, DATABASE);
if($mysqli->connect_errno){
echo "Connection error " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
if(!($stmt = $mysqli->prepare("UPDATE customer SET fName=?, lName=?, email=?, phone_number=?, address_no=?, address_street1=?,
address_street2=?, address_city=?, address_state=?, address_zip=? WHERE customer_id = ?"))){
echo "Prepare failed: " . $stmt->errno . " " . $stmt->error;
}
if(!($stmt->bind_param("isssiissssi",$_POST['customer_id'],$_POST['fName'],$_POST['lName'],$_POST['email'],$_POST['phone_number'], $_POST['address_no'],
$_POST['address_street1'],$_POST['address_street2'],$_POST['address_city'],$_POST['address_state'], $_POST['address_zip']))){
echo "Bind failed: " . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->execute()){
echo "Execute failed: " . $stmt->errno . " " . $stmt->error;
} else {
echo "Updated " . $stmt->affected_rows . " rows to customer.";
}
?>
Here is part of the code in my form:
<div>
<form method="post" action="updatecustomer.php">
<fieldset>
<legend>Update Existing Customer</legend>
<li>Customer ID:
<select name="customer_id">
<?php
if(!($stmt = $mysqli->prepare("SELECT customer_id, customer_id FROM customer"))){
echo "Prepare failed: " . $stmt->errno . " " . $stmt->error;
}
if(!$stmt->execute()){
echo "Execute failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
if(!$stmt->bind_result($customer_id, $customer_id)){
echo "Bind failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error;
}
while($stmt->fetch()){
echo '<option value=" '. $customer_id . ' "> ' . $customer_id . '</option>\n';
}
$stmt->close();
?>
</select>
</li>
<li>First Name: <input type="text" name="fName"> Last Name: <input type="text" name="lName"</li>
<li>Email Address: <input type="text" name="email"></li>
<li>Phone Number: <input type="text" name="phone_number"></li>
<li>Street Number: <input type="text" name="address_no"> Street Line 1: <input type="text" name="address_street1"></li>
<li>Street Line 2 (Apt or Unit Number): <input type="text" name="address_street2"></li>
<li>City: <input type="text" name="address_city"> State: <input type="text" name="address_state"> Zip: <input type="text" name="address_zip"> </li>
</fieldset>
<input type="submit" name="update" value="Update Customer">
<input type="submit" name="delete" value="Delete Customer">
</div>
Check the order of variables in your $stmt->bind_param line.
Try this: I have made the code neater, and easier to understand, and reordered the customer ID in the bind_param() method.
$stmt = $mysqli->prepare("
UPDATE customer
SET fName=?,
lName=?,
email=?,
phone_number=?,
address_no=?,
address_street1=?,
address_street2=?,
address_city=?,
address_state=?,
address_zip=?
WHERE customer_id = ?
");
if(!$stmt){
echo "Prepare failed: " . $stmt->errno . " " . $stmt->error;
}
$paramBinding = $stmt->bind_param("sssiissssii",
$_POST['fName'],
$_POST['lName'],
$_POST['email'],
$_POST['phone_number'],
$_POST['address_no'],
$_POST['address_street1'],
$_POST['address_street2'],
$_POST['address_city'],
$_POST['address_state'],
$_POST['address_zip'],
$_POST['customer_id']
);
if(!$paramBinding){
echo "Bind failed: " . $stmt->errno . " " . $stmt->error;
}
EDIT
Then when looping through the options:
foreach($stmt->fetch() AS $row){
echo '<option value="'. $row['customer_id'] . '"> ' . $row['customer_id'] . '</option>\n';
}
I have this problem while updating my database, so, it all works, i mean i have the form, it prints the values, but when i try to update it, everything gets updated apart from the username and password..
Here is the code i use..
Thanks!
if ($Act=='Save') {
mysql_query("BEGIN");
$sql = "Insert into tbl_galleries (gal_title,gal_image,username,password) Values (";
$sql.= "'". strip_tags(mysql_real_escape_string(trim($gal_title))). "','". strip_tags(mysql_real_escape_string(trim($gal_image))) ."','". strip_tags(mysql_real_escape_string(trim($username))). "',,'". strip_tags(mysql_real_escape_string(trim($password))). "',);";
$query = mysql_query($sql);
if(!$query){
mysql_query("ROLLBACK");
$myErrorsUpGr = mysql_error();
echo $myErrorsUpGr;
} else {
mysql_query("COMMIT");
echo 'Insertion was successfull.';
}
} else if ($Act=='Update'){
mysql_query("BEGIN");
$sql = " Update tbl_galleries set ";
$sql.= " gal_title='" . strip_tags(mysql_real_escape_string(trim($gal_title))) . "',";
$sql.= " gal_image='" . strip_tags(mysql_real_escape_string(trim($gal_image))) . "'";
$sql.= " where gal_id=" . $gal_id . ";";
$sql.= " username='" . strip_tags(mysql_real_escape_string(trim($username))) . "',";
$sql.= " password='" . strip_tags(mysql_real_escape_string(trim($password))) . "',";
<?php
include_once("db/envato_db.php");
if ($_SERVER['QUERY_STRING']!='')
{
$sql = "";
$sql = "SELECT gal_id,gal_title,gal_image,username,password FROM tbl_galleries where gal_id='" . $_REQUEST['gid'] ."';";
$query = mysql_query($sql) or $myErrorsP = mysql_error();
if(isset($myErrors) && $myErrorsP!='')
{
}
else
{
$row = mysql_fetch_row($query);
mysql_free_result($query);
$gal_id = $row[0];
$gal_title = $row[1];
$gal_image = $row[2];
$username = $row[3];
$password = $row[4];
}
}
?>
<tr>
<td width="104">Gallery Title:</td>
<td width="556"><input type="text" id="gtitle" name="gtitle" class="typeText" maxlength="50" value="<?php echo isset($gal_title)? $gal_title : ""?>" tabindex="1" /></td>
</tr>
<tr>
<td>Gallery Image:</td>
<td>
<input type="text" id="gimg" name="gimg" class="typeText" maxlength="100" value="<?php echo isset($gal_image)? $gal_image : ""?>" readonly/>
<input type="file" name="gimg_upl" id="gimg_upl"/>
Upload
</td>
</tr>
<tr>
<td width="104">Username:</td>
<td width="556"><input type="text" id="gusername" name="gusername" class="typeText" maxlength="50" value="<?php echo isset($username)? $username : ""?>" tabindex="1" /></td>
</tr>
<tr>
<td width="104">Password:</td>
<td width="556"><input type="text" id="gpassword" name="gpassword" class="typeText" maxlength="50" value="<?php echo isset($password)? $password : ""?>" tabindex="1" /></td>
</tr>
</table>
<table id="savetbl" style="width:680px;" cellpadding="3">
<tr>
<td align="center" colspan="2">
<?php
if(isset($gal_id) && $gal_id!='')
{
if(!isset($myErrorsP))
{
?>
<input type="button" value="» Update «" class="but" name="button" alt="Update" title="Update" onClick="Do_Update('Update', '<?php echo $gal_id?>');" tabindex="3">
<?php
}
}
else
{
if(!isset($myErrorsP))
{
?>
<input type="button" value="» Save «" class="but" name="button" alt="Save" title="Save" onClick="Do_Update('Save','0');" tabindex="3">
<?php
}
}
?>
</td>
</tr>
On your code you have this :
$sql.= " where gal_id=" . $gal_id . ";";
$sql.= " username='" . strip_tags(mysql_real_escape_string(trim($username))) . "',";
$sql.= " password='" . strip_tags(mysql_real_escape_string(trim($password))) . "',";
Try to set the where clause after the update of username & password :
$sql.= " username='" . strip_tags(mysql_real_escape_string(trim($username))) . "',";
$sql.= " password='" . strip_tags(mysql_real_escape_string(trim($password))) . "'";
$sql.= " where gal_id=" . $gal_id . ";";
Try this
$sql.= " where gal_id=" . $gal_id . ";";
$sql.= "AND username='" . strip_tags(mysql_real_escape_string(trim($username))) . "',";
$sql.= "AND password='" . strip_tags(mysql_real_escape_string(trim($password))) . "',";
Lines 22, 23, and 24 of your sample code