I'm back once again, I know you guys are probably tired of me lol but there are somethings I figured out how to add the text boxes. I am still having trouble with the drop down menu. I am supposed to have a drop down menu that has the list of the states and when you submit it, it will be abbreviated but that doesn't want to work for me either. I'm pretty sure it is something easy to fix and I keep over looking it.
<!DOCTYPE html>
<html>
<head>
<title>Lab 7, Part 1</title>
<meta charset="UTF-8"/>
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
</head>
<body>
<form name="myform" action="http://weblab.kennesaw.edu/formtest.php"
onsubmit="return validateForm()"
method = "post">
<h1 style="text-align:center">Lab 7, Part 1</h1>
<h2 style="text-align:center">IT 3203</h2>
<p style="text-align:center">Main Page!</p>
<table>
<th>Fruits For Sale!</th>
<tr><th>Fruits</th><th>Weight</th><th>Price</th></tr>
<?php
$db=mysqli_connect(null,null,null,'weblab')
or die("Can't connect to DB:" . mysqli_connect_error());
$q = " select fruit_item_no, fruit_name, fruit_weight, fruit_price";
$q .= " from fruit_t";
$q .= " order by fruit_name;";
$dbResult = mysqli_query($db,$q);
$num = mysqli_num_rows($dbResult);
if ($num == 0) {
echo '<tr><td colspan="2">';
echo 'Database query retrieved zero rows.</td></tr>';
}
while ($row = mysqli_fetch_assoc($dbResult)) {
$name = $row['fruit_name'];
$weight = $row['fruit_weight'];
$price = $row['fruit_price'];
echo "<tr><td><b>$name</b></td>";
echo "<td>$weight</td>";
echo "<td>$price</td>";
echo "<td><input type='text' name='name'></td></tr>\n";
}
?>
</table>
<br>
<label>First Name
<input type="text"
name="firstname" id="firstname"
size="25" />
</label>
<br>
<br>
<label>Last Name
<input type="text"
name="lastname" id="lastname"
size="25" />
</label>
<br>
<br>
<label>Street Address
<input type="text"
name="streetaddress" id="streetaddress"
size="35" />
</label>
<br>
<br>
<label>City
<input type="text"
name="city" id="city"
size="25" />
</label>
<label>State:
<select name="state" id="state">
<?php
$db=mysqli_connect(null,null,null,'weblab')
or die("Can't connect to DB:" . mysqli_connect_error());
$q = " select state_abbr, state_name";
$q .= " from state_t";
$q .= " order by state_name;";
while ($x = mysqli_fetch_assoc($dbResult)) {
$state_abbr = $x["state_abbr"];
$state_name = $x["state_name"];
?>
<option value="<?php echo $state_abbr; ?>">
<?php echo $state_name; ?></option>
<?php
}
?>
</select>
<?php
}
?>
</label>
<br>
<br>
<label>Zip code:
<input type="text"
name="zipcode" id="zipcode"
size="20" />
</label>
<br>
<br>
<label>Visa
<input type="radio" name="pref_payment"
id="pref_payment_visa" value="visa" checked />
</label><br>
<label>MasterCard
<input type="radio" name="pref_payment"
id="pref_payment_master" value="master" checked />
</label><br>
<label>American Express
<input type="radio" name="pref_payment"
id="pref_payment_american" value="american" checked />
</label><br>
<input type="submit" value="Submit!">
</form>
</body>
</html>
Try this:
$db=mysqli_connect(null,null,null,'weblab')
or die("Can't connect to DB:" . mysqli_connect_error());
$q = " select fruit_item_no, fruit_name, fruit_weight, fruit_price";
$q .= " from fruit_t";
$q .= " order by fruit_name;";
$dbResult = mysqli_query($db,$q);
$num = mysqli_num_rows($dbResult);
if ($num == 0) {
echo '<tr><td colspan="2">';
echo 'Database query retrieved zero rows.</td></tr>';
}
while ($row = mysqli_fetch_assoc($dbResult)) {
$name = $row['fruit_name'];
$weight = $row['fruit_weight'];
$price = $row['fruit_price'];
echo "<tr><td><b>$name</b></td>";
echo "<td>$weight</td>";
echo "<td>$price</td>";
echo "<td><input type='text' name='name'></td></tr>\n";
?>
</table>
<label>State:
<select name="state" id="state">
<?php
$db=mysqli_connect(null,null,null,'weblab')
or die("Can't connect to DB:" . mysqli_connect_error());
$q = " select state_abbr, state_name";
$q .= " from state_t";
$q .= " order by state_name;";
$dbResult = mysqli_query($db,$q);
while ($x = mysqli_fetch_assoc($dbResult)) {
$state_abbr = $x["state_abbr"];
$state_name = $x["state_name"];
?>
<option value="<?php echo $state_id; ?>">
<?php echo $state_name; ?></option>
<?php
}
?>
</select>
</label>
That is why code indentation is so important.. You were missing closing PHP tags at two places. Refer to below code.
<!DOCTYPE html>
<html>
<head>
<title>Lab 7, Part 1</title>
<meta charset="UTF-8"/>
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
</head>
<body>
<form name="myform" action="http://weblab.kennesaw.edu/formtest.php" onsubmit="return validateForm()" method = "post">
<h1 style="text-align:center">Lab 7, Part 1</h1>
<h2 style="text-align:center">IT 3203</h2>
<p style="text-align:center">Main Page!</p>
<table>
<th>Fruits For Sale!</th>
<tr><th>Fruits</th><th>Weight</th><th>Price</th></tr>
<?php
$db = mysqli_connect(null,null,null,'weblab')
or die("Can't connect to DB:" . mysqli_connect_error());
$q = " select fruit_item_no, fruit_name, fruit_weight, fruit_price";
$q .= " from fruit_t";
$q .= " order by fruit_name;";
$dbResult = mysqli_query($db,$q);
$num = mysqli_num_rows($dbResult);
if ($num == 0)
{
echo '<tr><td colspan="2">';
echo 'Database query retrieved zero rows.</td></tr>';
}
while ($row = mysqli_fetch_assoc($dbResult))
{
$name = $row['fruit_name'];
$weight = $row['fruit_weight'];
$price = $row['fruit_price'];
echo "<tr><td><b>$name</b></td>";
echo "<td>$weight</td>";
echo "<td>$price</td>";
echo "<td><input type='text' name='name'></td></tr>\n";
}
?>
</table>
<br>
<label>First Name
<input type="text" name="firstname" id="firstname" size="25" />
</label>
<br>
<br>
<label>Last Name
<input type="text" name="lastname" id="lastname" size="25" />
</label>
<br>
<br>
<label>Street Address
<input type="text" name="streetaddress" id="streetaddress" size="35" />
</label>
<br>
<br>
<label>City
<input type="text" name="city" id="city" size="25" />
</label>
<label>State:
<select name="state" id="state">
<?php
$db = mysqli_connect(null,null,null,'weblab')
or die("Can't connect to DB:" . mysqli_connect_error());
$q = " select state_abbr, state_name";
$q .= " from state_t";
$q .= " order by state_name;";
$dbResult_state = mysqli_query($db,$q);
while ($x = mysqli_fetch_assoc($dbResult_state))
{
$state_abbr = $x["state_abbr"];
$state_name = $x["state_name"];
?>
<option value="<?php echo $state_abbr; ?>">
<?php echo $state_name; ?></option>
<?php
} ?>
</select>
<?php } ?> <!-- remove this -->
</label>
<br>
<br>
<label>Zip code:
<input type="text" name="zipcode" id="zipcode" size="20" />
</label>
<br>
<br>
<label>Visa
<input type="radio" name="pref_payment" id="pref_payment_visa" value="visa" checked />
</label>
<br>
<label>MasterCard
<input type="radio" name="pref_payment" id="pref_payment_master" value="master" checked />
</label>
<br>
<label>American Express
<input type="radio" name="pref_payment" id="pref_payment_american" value="american" checked />
</label>
<br>
<input type="submit" value="Submit!">
</form>
</body>
</html>
Related
I wish to display the record to the user that I am deleting in PHP?
I am trying to display the record in a form that I want to delete but only the input type = text values are being displayed and the radio button values are not being displayed?
<?php
require('/home/s3022041/sqlC/dbConnect.php');
if(isset($_POST['search']))
{
$search = mysqli_real_escape_string($connection, $_POST['id']);
$id = $_POST['id'];
$stp1 = preg_replace("/[^a-zA-Z0-9]/", "", $id); //grab only the alphanumerics
$stp2 = strtoupper($stp1); //Make all alphabets uppercase
$stp3 = preg_replace('/\d+/', '',$stp2); //extract the alphabets part
$newsearchid = str_replace($stp3,"-".$stp3."-",$stp2); //put hyphens before and after the alphabet part
$query = "SELECT * FROM cars WHERE Registration_Number = '$newsearchid' ";
$query_run = mysqli_query($connection, $query);
while($row = mysqli_fetch_array($query_run))
{
?>
<form action="delete_process.php" method="POST" class="form">
<h2>Car Registration Form</h2>
<div class="mb-3">
<input type="radio" name="Car" id="Toyota" value=" <?php echo $row['make']; ?>" />
<label for="Toyota" class="form-label">Toyota</label>
<select name="Models[Toyota]" class="form-control" value=" <?php echo $row['model']; ?>">
<option value="">None</option>
<option value="Camri">Camri</option>
<option value="Corolla">Corolla</option>
<option value="Estima">Estima</option>
</select><br>
<input type="radio" name="Car" id="Nissan" value=" <?php echo $row['make']; ?>"/>
<label for="Nissan" class="form-label">Nissan</label>
<select name="Models[Nissan]" class="form-control" value=" <?php echo $row['model']; ?>">
<option value="">None</option>
<option value="Micra">Micra</option>
<option value="roller">roller</option>
<option value="fushe">fushe</option>
</select><br>
<input type="radio" name="Car" id="Mercedez" value=" <?php echo $row['make']; ?>"/>
<label for="Mercedez" class="form-label">Mercedez</label>
<select name="Models[Mercedez]" class="form-control" value=" <?php echo $row['model']; ?>">
<option value="">None</option>
<option value="5series">5series</option>
<option value="x7">x7</option>
<option value="x5">x5</option>
</select><br>
<label for="vehicleidentificationnumber" class="form-label">vehicle identification
number(VIN)</label>
<input type="text" disabled="disabled" name="vehicleidentificationnumber" class="form-control" maxlength=20 value=" <?php echo $row['VIN']; ?>" required >
<br>
<label for="ManufacturingYear" class="form-label">Manufacturing Year</label>
<input type="text" name="Manufacture_Year" class="form-control" maxlength=20 value=" <?php echo $row['Manufacture_Year']; ?>" required >
<label for="Enginesize" class="form-label">Engine size</label>
<input type="text" name="Engine_Size" class="form-control" maxlength=20 value=" <?php echo $row['Engine_Size']; ?>" required>
<label for="TransmissionType" class="form-label">TransmissionType</label><br>
<input type="radio" name="Transmission_Type" id="TransmissionType" value=" <?php echo $row['Transmission_Type']; ?>" />
<label for="Automatic" class="form-label">Automatic</label><br>
<input type="radio" name="Transmission_Type" id="TransmissionType" value=" <?php echo $row['Transmission_Type']; ?>" />
<label for="Manual" class="form-label">Manual</label><br>
<input type="radio" name="Transmission_Type" id="TransmissionType" value=" <?php echo $row['Transmission_Type']; ?>" />
<label for="SemiAutomatic" class="form-label">Semi-Automatic</label><br>
<label for="NoofSeats" class="form-label">No. of Seats</label>
<input type="number" name="NoOfSeats" class="form-control" value="<?php echo $row['NoOfSeats']; ?>" maxlength=20 required>
<label for="Noofdoors" class="form-label">No. of doors</label>
<input type="number" name="NoOfDoors" class="form-control" maxlength=20 value="<?php echo $row['NoOfDoors']; ?>" required>
<label for="Fueltype" class="form-label">Fuel type</label>
<input type="text" name="Fuel_Type" class="form-control" maxlength=20 value=" <?php echo $row['Fuel_Type']; ?>" required>
<label for="Colour" class="form-label">Colour</label>
<input type="text" name="Colour" class="form-control" maxlength=20 value=" <?php echo $row['Colour']; ?>" required>
<label for="RegistrationNumber" class="form-label">Registration Number (use Dublin
registration) </label>
<input type="hidden" disabled="disabled" name="Registration_Number" class="form-control" maxlength=20 value=" <?php echo $row['Registration_Number']; ?>" required>
<label for="Dateoffirstregistration" class="form-label">Date of first registration</label>
<input type="date" name="DateOfRegestration" class="form-control" maxlength=20 value=" <?php echo $row['DateOfRegestration']; ?>" required>
</div>
<button type="submit" name="search" class="btn btn-primary">Delete</button>
</form>
<?php
}
}
else{
echo "<h1> No records found </h1>";
echo "<a href='index.php'>home</a>";
}
?>
</div>
<div class="modal-footer">
</form>
Here I am trying to delete the above record that is displayed but it says deleted successfully but when I go and check the records it is not deleted it's still there?
<?php
require('/home/s3022041/sqlC/dbConnect.php');
if(isset($_POST['search']))
{
$search = mysqli_real_escape_string($connection, $_POST['search']);
$id = $_POST['Registration_Number'];
$query = "DELETE FROM `cars` WHERE Registration_Number='$id' ";
$query_run = mysqli_query($connection, $query) or die ("not done");
if($query_run)
{
echo "<h1> deleted successfully</h1>";
echo "<a href='index.php'>home</a>";
}
else
{
echo "<h1> not deleted </h1>";
echo "<a href='index.php'>home</a>";
echo 'Error! ' . mysqli_error($connection);
}
}
Change
1. Form
<input type="radio" name="Car" id="Toyota" value="<?=$row['make']; ?>" />
to
<input type="radio" name="Car" id="Toyota" <?=($row['make'] == 'Toyota' ? 'value="'.$row['make'].'" checked':NULL); ?> />
<!-- or better yet -->
<input type="radio" name="Car" id="Toyota" value="Toyota" <?=($row['make'] == 'Toyota' ? 'checked':NULL); ?> />
2. Database
File: delete_process.php
// Change
if($query_run) // << This only tell the script the sql statement run with no errors
// To
if(mysql_affected_rows()<=1) // << This confirms that one or more rows where changed by the sql statement
Explained
PHP Shorthand If/Else Using Ternary Operators (?:)
/* basic usage */
$var = 5;
$var_is_greater_than_two = ($var > 2 ? true : false); // returns true
Reason for error
If a radio button are not being displayed, it most-likely an 'Undefined variable' when getting $row['make'].
Other thing, remove the white spacing from. As this may play a part when using a MYSQL Statement.
<!-- From -->
value=" <?php echo $row['Registration_Number']; ?>"
<!-- To -->
value="<?=$row['Registration_Number']; ?>"
*Shorthand one line echo/print <?=
My code is:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>SomuFinance - Personal Finance Manager</title>
<link rel="stylesheet" type="text/css" href="indexStyle.css">
<script src="scripts/jquery-3.1.0.min.js"></script>
<script type="text/javascript" src="scripts/jquery.validate.min.js"></script>
<style type="text/css">
#addItemContainer {
background-color: rgba(204,207,232,1);
}
</style>
<script type="text/javascript">
var flag=0;
</script>
</head>
<body>
<form id="list" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div id="container">
<input type="submit" class="button" name="edit" id="edit" value="Edit" />
<input type="button" class="button" name="delete" value="Delete" />
<input type="hidden" id="action" name="action">
<table id="listDB">
<tr>
<th>Select</th>
<th>ID</th>
<th>Category ID</th>
<th>Shop</th>
<th>Item</th>
<th>Quantity</th>
<th>Unit</th>
<th>Price Based On</th>
<th>MRP</th>
<th>Seller's Price</th>
<th>Last Updated On</th>
</tr>
<?php
$dbc = mysqli_connect('localhost','root','atlantis2016','itemDB')
or die("Error Connecting to Database");
if(isset($_POST['confirmDelete']))
{
if($_POST['action']=='confirmDelete')
{
foreach ($_POST['selected'] as $delete_id)
{
$query = "DELETE FROM grocery WHERE id = $delete_id";
mysqli_query($dbc, $query)
or die('Error querying database.');
}
}
}
$query1 = "SELECT DISTINCT category FROM grocery";
$result1 = mysqli_query($dbc, $query1)
or die("Error Querying Database");
while($row = mysqli_fetch_array($result1))
{
$category = $row['category'];
$query2 = "SELECT * FROM grocery WHERE category='$category' ORDER BY item ASC";
$result2 = mysqli_query($dbc, $query2)
or die("Error Querying Database");
echo '<tr>';
echo '<td class="catHead" colspan=11>'.$category.'</td>';
echo '</tr>';
$catCount=1;
while($inRow = mysqli_fetch_array($result2))
{
$id = $inRow['id'];
$shop = $inRow['shop'];
$item = $inRow['item'];
$qnty = $inRow['quantity'];
$unit = $inRow['unit'];
$price_based_on = $inRow['price_based_on'];
$mrp = $inRow['MRP'];
$sellers_price = $inRow['sellers_price'];
$last_updated_on = $inRow['last_updated_on'];
echo '<tr>';
echo '<td><input type="checkbox" id="selected" value="' . $id . '" name="selected[]" /></td>';
echo '<td>'.$id.'</td>';
echo '<td>'.$catCount.'</td>';
echo '<td>'.$shop.'</td>';
echo '<td class="leftAligned">'.$item.'</td>';
echo '<td>'.$qnty.'</td>';
echo '<td>'.$unit.'</td>';
echo '<td>'.$price_based_on.'</td>';
echo '<td class="pri">₹'.$mrp.'</td>';
echo '<td class="pri">₹'.$sellers_price.'</td>';
echo '<td>'.$last_updated_on.'</td>';
echo '</tr>';
$catCount++;
}
}
?>
</table>
</div>
<div class="dialogBG">
<div id="deleteConfirmDialog" class="dialog">
<div class="closeDialog"></div>
<p>Sure you want to delete the selected Data?</p>
<input type="submit" id="confirmDelete" class="dialogButton" name="confirmDelete" value="Delete" />
<input type="button" id="cancelDelete" class="dialogButton cancelButton" name="cancelDelete" value="Cancel" />
</div>
</div>
</form>
<div class="dialogBG">
<div id="addItemContainer" class="dialog">
<div class="closeDialog"></div>
<h1>Edit Item</h1>
<form id="data" method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<?php
if(isset($_POST['action']))
{
if($_POST['action']=='edit')
{
echo '<script>flag=1;</script>';
foreach ($_POST['selected'] as $edit_id)
{
$query = "SELECT * FROM grocery WHERE id = $edit_id";
$result = mysqli_query($dbc, $query)
or die('Error querying database.');
break;
}
$inRow = mysqli_fetch_array($result);
$id = $inRow['id'];
$shop = $inRow['shop'];
$category = $inRow['category'];
$item = $inRow['item'];
$qnty = $inRow['quantity'];
$unit = $inRow['unit'];
$price_based_on = $inRow['price_based_on'];
$mrp = $inRow['MRP'];
$sellers_price = $inRow['sellers_price'];
$last_updated_on = $inRow['last_updated_on'];
}
}
?>
<div class="leftAligned">
<div class="inp">
<label for="shop">ID : </label>
<input type="text" id="id" name="id" value="<?php echo $id; ?>" required disabled>
</div> <br>
<div class="inp">
<label for="shop">Shop : </label>
<input type="text" id="shop" name="shop" value="<?php echo $shop; ?>" required>
</div> <br>
<div class="inp">
<label for="category">Category : </label>
<input type="text" id="category" name="category" value="<?php echo $category; ?>" required>
</div> <br>
<div class="inp">
<label for="item">Item : </label>
<input type="text" id="item" name="item" value="<?php echo $item; ?>" required>
</div> <br>
<div class="inp">
<label for="qnty">Quantity : </label>
<input type="text" id="qnty" name="qnty" value="<?php echo $qnty; ?>" required>
</div> <br>
<div class="inp">
<label for="unit">Unit : </label>
<input type="text" id="unit" name="unit" value="<?php echo $unit; ?>" required>
</div> <br>
<div class="inp">
<label for="price_based_on">Price based on : </label>
<select name="price_based_on" id="price_based_on">
<option value="kilos">Kilos</option>
<option value="packet">Packet</option>
<option value="bottle">Bottle</option>
<option value="box">Box</option>
<option value="piece">Piece</option>
</select>
</div> <br>
<div class="inp">
<label for="mrp">MRP (₹) : </label>
<input type="text" id="mrp" name="mrp" value="<?php echo $mrp; ?>" required>
</div> <br>
<div class="inp">
<label for="sellers_price">Seller's Price (₹) : </label>
<input type="text" id="sellers_price" value="<?php echo $sellers_price; ?>" name="sellers_price" required>
</div> <br>
<div class="inp">
<label for="last_updated_on">Last Updated on : </label>
<input type="date" id="last_updated_on" name="last_updated_on" value="<?php echo $last_updated_on; ?>" required>
</div>
</div>
<div class="inp">
<input id="insertButton" type="submit" name="submit" value="Insert">
</div>
<div id="message">
<?php
if(isset($_POST['submit']))
{
echo "<script> alert('$id'); </script>";
$shop = $_POST['shop'];
$category = $_POST['category'];
$item = $_POST['item'];
$qnty = $_POST['qnty'];
$unit = $_POST['unit'];
$price_based_on = $_POST['price_based_on'];
$mrp = $_POST['mrp'];
$sellers_price = $_POST['sellers_price'];
$last_updated_on = $_POST['last_updated_on'];
$result=null;
$query = "UPDATE grocery SET shop='$shop', category='$category', item='$item', quantity='$qnty', unit='$unit', price_based_on='$price_based_on', mrp='$mrp', sellers_price='$sellers_price', last_updated_on='$last_updated_on' WHERE id='$id'";
if(!empty($shop)&&!empty($category)&&!empty($item)&&is_numeric($qnty)&&!empty($unit)&&is_numeric($mrp)&&is_numeric($sellers_price)&&!empty($last_updated_on))
{
$result = mysqli_query($dbc, $query)
or die(mysqli_error($dbc));
}
if($result)
{
echo '<span class="success">Item Edited Successfully!</span>';
}
else
{
echo '<span class="failure">Failed to insert Item.</span>';
}
//header("Refresh:2");
}
?>
<script>
$(document).ready(function(){
$( "#data" ).validate({
rules: {
qnty: {
number: true
},
mrp: {
number: true
},
sellers_price: {
number: true
}
},
messages: {
qnty : {
number: '<br> <span class="failure err">Enter a valid quantity</span>'
},
mrp : {
number: '<br> <span class="failure err">Enter a valid MRP</span>'
},
sellers_price : {
number: '<br> <span class="failure err">Enter a valid Price</span>'
},
}
});
});
</script>
</div>
</form>
</div>
</div>
<script type="text/javascript">
$(document).ready(function(){
$('.button').click(function(event){
if($(this).val()=="Delete")
{
$("#deleteConfirmDialog").show(200).parent(".dialogBG").fadeIn(200);
$("#action").val('confirmDelete');
}
else if($(this).val()=="Edit")
{
event.preventDefault();
$("#action").val('edit');
$("#list").submit();
}
});
if(flag===1)
{
console.log("This shouldn't be there if the page reloads!");
$("#addItemContainer").show(200).parent(".dialogBG").fadeIn(200);
}
$('#confirmDelete').click(function(){
$(".closeDialog").trigger("click");
});
$('#cancelDelete').click(function(){
$("input:checkbox[name='selected[]']").prop('checked', false);
});
$(".closeDialog").click(function (e){
$(this).parent(".dialog").hide('200').parent(".dialogBG").fadeOut('200');
});
$(".cancelButton").click(function (e){
$(this).parent(".dialog").hide('200').parent(".dialogBG").fadeOut('200');
});
});
</script>
<?php
mysqli_close($dbc);
?>
</body>
</html>
Notice that I obtain the id of the first selected element by using the lines:
foreach ($_POST['selected'] as $edit_id)
{
$query = "SELECT * FROM grocery WHERE id = $edit_id";
$result = mysqli_query($dbc, $query)
or die('Error querying database.');
break;
}
$inRow = mysqli_fetch_array($result);
$id = $inRow['id'];
Now, I use the id(which is exactly the same as edit_id) thus obtained to access the record in the DB, and show it in the popup.
<div class="leftAligned">
<div class="inp">
<label for="shop">ID : </label>
<input type="text" id="id" name="id" value="<?php echo $id; ?>" required disabled>
</div> <br>
<div class="inp">
<label for="shop">Shop : </label>
<input type="text" id="shop" name="shop" value="<?php echo $shop; ?>" required>
</div> <br>
<div class="inp">
<label for="category">Category : </label>
<input type="text" id="category" name="category" value="<?php echo $category; ?>" required>
</div> <br>
<div class="inp">
<label for="item">Item : </label>
<input type="text" id="item" name="item" value="<?php echo $item; ?>" required>
</div> <br>
<div class="inp">
<label for="qnty">Quantity : </label>
<input type="text" id="qnty" name="qnty" value="<?php echo $qnty; ?>" required>
</div> <br>
<div class="inp">
<label for="unit">Unit : </label>
<input type="text" id="unit" name="unit" value="<?php echo $unit; ?>" required>
</div> <br>
<div class="inp">
<label for="price_based_on">Price based on : </label>
<select name="price_based_on" id="price_based_on">
<option value="kilos">Kilos</option>
<option value="packet">Packet</option>
<option value="bottle">Bottle</option>
<option value="box">Box</option>
<option value="piece">Piece</option>
</select>
</div> <br>
<div class="inp">
<label for="mrp">MRP (₹) : </label>
<input type="text" id="mrp" name="mrp" value="<?php echo $mrp; ?>" required>
</div> <br>
<div class="inp">
<label for="sellers_price">Seller's Price (₹) : </label>
<input type="text" id="sellers_price" value="<?php echo $sellers_price; ?>" name="sellers_price" required>
</div> <br>
<div class="inp">
<label for="last_updated_on">Last Updated on : </label>
<input type="date" id="last_updated_on" name="last_updated_on" value="<?php echo $last_updated_on; ?>" required>
</div>
</div>
The ID and all the relevant details thus far are exactly as they should be.
However, further down, when I try to use the ID to update the record, using the statement
UPDATE grocery SET shop='$shop', category='$category', item='$item',
quantity='$qnty', unit='$unit', price_based_on='$price_based_on',
mrp='$mrp', sellers_price='$sellers_price',
last_updated_on='$last_updated_on'
WHERE id='$id'
somehow the id has changed to the last value of id in the table. So, instead of updating the table's correct record with the updated values, since the id "magically" changes at this point (after the sumbit button has been pressed), the last value of the id appears. I want the id to stay the same as the original id.
Why is this happening and how can I solve this?
I am trying to fill a html form with data being received out of my mysql database. However I cannot set the forms to display on-load the variables being extracted from the database. I would like the form on-load to hold the data last entered into the forms which have been added to the database previously.
$query = "SELECT FROM character_tbl WHERE character_player
='".$_SESSION["user"]."' character_tbl";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
$name = $row['character_name'];
$race = $row['character_race'];
$class = $row['character_class'];
$alignment = $row['character_alignment'];
$hp = $row['character_hp'];
$str = $row['character_str'];
$dex = $row['character_dex'];
$con = $row['character_con'];
$int = $row['character_int'];
$wis = $row['character_wis'];
$cha = $row['character_cha'];
$ac = $row['character_ac'];
$touch = $row['character_touch'];
$flat = $row['character_flat'];
$fort = $row['character_fort'];
$ref = $row['character_ref'];
$will = $row['character_will'];
}
echo $will;
mysql_close();
?>
<!DOCTYPE html>
<html>
<body>
<div id="nav">
<form action="user.php">
<input type="submit" value="Back">
</form>
</div>
<div id="section">
<form action="update.php" method="POST">
Character Name:<br>
<input type="text" name="name" value="<?php echo $name;?>">
<br>
Race<br>
<input type="text" name="race" value="<?php echo $race;?>">
<br>
Class<br>
<input type="text" name="class" value="<?php echo $class;?>">
<br>
Alignment<br>
<input type="text" name="alignment" value="<?php echo $alignment;?>">
<br>
HP<br>
<input type="text" name="hp" value="<?php echo $hp;?>">
<br>
STR<br>
<input type="number" name="str" value="<?php echo $str;?>">
<br>
DEX<br>
<input type="number" name="dex" value="<?php echo $dex;?>">
<br>
CON<br>
<input type="text" name="con" value="<?php echo $con;?>">
<br>
INT<br>
<input type="text" name="int" value="<?php echo $int;?>">
<br>
WIS<br>
<input type="text" name="wis" value="<?php echo $wis;?>">
<br>
CHA<br>
<input type="text" name="cha" value="<?php echo $cha;?>">
<br>
AC<br>
<input type="text" name="ac" value="<?php echo $ac;?>">
<br>
Touch AC<br>
<input type="text" name="touch" value="<?php echo $touch;?>">
<br>
Flat-Footed AC<br>
<input type="text" name="flat" value="<?php echo $flat;?>">
<br>
Fortitude<br>
<input type="text" name="fort" value="<?php echo $fort;?>">
<br>
Reflex<br>
<input type="text" name="ref" value="<?php echo $ref;?>">
<br>
Will<br>
<input type="text" name="will" value="<?php echo $will;?>">
</br>
<input type="submit" value="Update">
</form>
I think the SQL has error:
SELECT FROM character_tbl WHERE character_player
try:
SELECT * FROM character_tbl WHERE character_player
You have syntax error in your mysql query. You have not place field or columns name or (*) for all columns to extract.
Try like this..
$query = "SELECT * FROM character_tbl WHERE character_player ='".$_SESSION['user']."'";
I have the form that is for users to edit their profiles:
<div id="page">
<?php echo message(); ?>
<?php echo form_errors($errors); ?>
<h2>Edit Admin: <?php echo htmlentities($admin["username"]); ?></h2>
<form action="edit_admin.php?usr_serno=<?php echo urlencode($admin["usr_serno"]); ?>" method="post">
<p>Username:
<input type="text" name="username" value="<?php echo htmlentities($admin["username"]); ?>" />
</p>
<p>Password:
<input type="password" name="password" value="" />
<?php if (!isset($_GET["password"])
</p>
<p>Email:
<input type="text" name="email" value="<?php echo htmlentities($admin["email"]); ?>" />
</p>
<p>Role:
<select type="int" name="role_serno">
<option value="1">Administrator</option>
<option value="2">User</option>
</select>
</p>
<p>First Name:
<input type="text" name="name" value="<?php echo htmlentities($admin["name"]); ?>" />
</p>
<p>Last Name:
<input type="text" name="lastname" value="<?php echo htmlentities($admin["lastname"]); ?>" />
</p>
<p>ID:
<input type="text" name="id" value="<?php echo htmlentities($admin["id"]); ?>" />
</p>
<p>Address:
<input type="text" name="address" value="<?php echo htmlentities($admin["address"]); ?>" />
</p>
<p>Postcode:
<input type="text" name="postcode" value="<?php echo htmlentities($admin["postcode"]); ?>" />
</p>
<p>City:
<select id="city" type="text" name="city" value="<?php echo htmlentities($admin["city"]); ?>">
<option value="Nicosia">Nicosia</option>
<option value="Limassol">Limassol</option>
<option value="Larnaca">Larnaca</option>
<option value="Paphos">Paphos</option>
<option value="Other">Other</option>
</select>
<input id="other_city" type="text" name="other_city" value="<?php echo htmlentities($admin["city"]); ?>" />
</p>
<p>Telephone:
<input type="text" name="telephone" value="<?php echo htmlentities($admin["telephone"]); ?>" />
</p>
<p>College:
<input type="text" name="college" value="<?php echo htmlentities($admin["college"]); ?>" />
</p>
<input type="submit" name="submit" value="Edit Admin" />
</form>
<br />
Cancel
</div>
`
And on top of that page i am checking and trying to update the users profile:
if (isset($_POST["submit"])) {
$required_fields = array("username","email","role_serno",
"name","lastname","id","telephone","address","college","postcode","city");
validate_presences($required_fields);
$fields_with_max_lengths = array("username" => 30);
validate_max_lengths($fields_with_max_lengths);
if (empty($errors)) {
// Perform Update
//if (!isset ($_POST["password"]))
//$hashed_password = $_GET["password"];
//if (isset ($_POST["password"]))
$id_serno = $admin["usr_serno"];
$username = mysql_prep($_POST["username"]);
$hashed_password = password_encrypt($_POST["password"]);
$email=mysql_prep($_POST["email"]);
$role_serno=mysql_prep($_POST["role_serno"]);
$name=mysql_prep($_POST["name"]);
$lastname=mysql_prep($_POST["lastname"]);
$id=mysql_prep( $_POST["id"]);
$date_create=mysql_prep( $_POST["date_create"]);
$address=mysql_prep( $_POST["address"]);
$postcode=mysql_prep( $_POST["postcode"]);
$city=mysql_prep( $_POST["city"]);
$telephone=mysql_prep( $_POST["telephone"]);
$college=mysql_prep( $_POST["college"]);
$query = "UPDATE users SET ";
$query .= "username = '{$username}', ";
$query .= "password = '{$hashed_password}', ";
$query .= "email = '{$email}', ";
$query .= "role_serno = '{$role_serno}', ";
$query .= "name = '{$name}', ";
$query .= "lastname = '{$lastname}', ";
$query .= "id = '{$id}', ";
$query .= "address = '{$address}', ";
$query .= "postcode = '{$postcode}', ";
$query .= "city = '{$city}', ";
$query .= "telephone = '{$telephone}', ";
$query .= "college = '{$college}' ";
$query .= "WHERE usr_serno = {$id_serno} ";
$query .= "LIMIT 1";
$result = mysqli_query($connection, $query);
if ($result && mysqli_affected_rows($connection) == 1) {
// Success
$_SESSION["message"] = "Admin updated.";
redirect_to("manage_admins.php");
} else {
// Failure
$_SESSION["message"] = "Admin update failed.";
}
}
} else {
// This is probably a GET request
} // end: if (is set($_POST["submit"]))`
the issue comes when the user is posting a form without passing the password in cause then the database is updated with empty password and i have a check not to accept empty passwords at lo-gin page.
Is there a way to avoid and check this.
Please advise cause I am new with developing in php and I am not 100% sure how this is going to be done.
I tried so far to check if post value is empty then pass the current database value without encoding it.. but for some reason the if check doesn't actually do anything
you can check like this
if (isset($_POST["submit"]) && trim($_POST['password']) !="") {
//run the code
}else{
}
You can add a client side javascript validation here.
In case of server side
check
if(empty($_POST['password']))
{
// whatever you want to ask and do
}
This question is relating to 2 php scripts.
The first script is called pick_modcontact.php where I choose a contact (from a contact book like phone book), then posts to the script show_modcontact.php When I click the submit button on the form on pick.modcontact.php. As a result of submitting the form I am then taken to show_modcontact.php. As the variables are not present the user is directed back to pick_modcontact.php
I can not work out how to correct the code so that it will show the results of the script show_modcontact.php
This script shows all contacts in a database which is an "address book" this part works fine. please see below.
Name:pick_modcontact.php
if ($_SESSION['valid'] != "yes") {
header( "Location: contact_menu.php");
exit;
}
$db_name = "testDB";
$table_name = "my_contacts";
$connection = #mysql_connect("localhost", "admin", "user") or die(mysql_error());
$db = #mysql_select_db($db_name, $connection) or die(mysql_error());
$sql = "SELECT id, f_name, l_name FROM $table_name ORDER BY f_name";
$result = #mysql_query($sql, $connection) or die(mysql_error());
$num = #mysql_num_rows($result);
if ($num < 1) {
$display_block = "<p><em>Sorry No Results!</em></p>";
} else {
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
$f_name = $row['f_name'];
$l_name = $row['l_name'];
$option_block .= "<option value\"$id\">$f_name, $l_name</option>";
}
$display_block = "<form method=\"POST\" action=\"show_modcontact.php\">
<p><strong>Contact:</strong>
<select name=\"id\">$option_block</select>
<input type=\"submit\" name=\"submit\" value=\"Select This Contact\"></p>
</form>";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Modify A Contact</title>
</head>
<body>
<h1>My Contact Management System</h1>
<h2><em>Modify a Contact</em></h2>
<p>Select a contact from the list below, to modify the contact's record.</p>
<? echo "$display_block"; ?>
<br>
<p>Return to Main Menu</p>
</body>
</html>
This script is for modifying the contact: named show_modcontact.php
<?php
if (!$_POST['id']) {
header( "Location: pick_modcontact.php");
exit;
} else {
session_start();
}
if ($_SESSION['valid'] != "yes") {
header( "Location: pick_modcontact.php");
exit;
}
$db_name = "testDB";
$table_name = "my_contacts";
$connection = #mysql_connect("localhost", "admin", "pass") or die(mysql_error());
$db = #mysql_select_db($db_name, $connection) or die(mysql_error());
$sql = "SELECT f_name, l_name, address1, address2, address3, postcode, prim_tel, sec_tel, email, birthday FROM $table_name WHERE id = '" . $_POST['id'] . "'";
$result = #mysql_query($sql, $connection) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$f_name = $row['f_name'];
$l_name = $row['l_name'];
$address1 = $row['address1'];
$address2 = $row['address2'];
$address3 = $row['address3'];
$country = $row['country'];
$prim_tel = $row['prim_tel'];
$sec_tel = $row['sec_tel'];
$email = $row['email'];
$birthday = $row['birthday'];
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Modify A Contact</title>
</head>
<body>
<form action="do_modcontact.php" method="post">
<input type="text" name="id" value="<? echo $_POST['id']; ?>" />
<table cellpadding="5" cellspacing="3">
<tr>
<th>Name & Address Information</th>
<th> Other Contact / Personal Information</th>
</tr>
<tr>
<td align="top">
<p><strong>First Name:</strong><br />
<input type="text" name="f_name" value="<? echo "$f_name"; ?>" size="35" maxlength="75" /></p>
<p><strong>Last Name:</strong><br />
<input type="text" name="l_name" value="<? echo "$l_name"; ?>" size="35" maxlength="75" /></p>
<p><strong>Address1:</strong><br />
<input type="text" name="f_name" value="<? echo "$address1"; ?>" size="35" maxlength="75" /></p>
<p><strong>Address2:</strong><br />
<input type="text" name="f_name" value="<? echo "$address2"; ?>" size="35" maxlength="75" /></p>
<p><strong>Address3:</strong><br />
<input type="text" name="f_name" value="<? echo "$address3"; ?>" size="35" maxlength="75" /> </p>
<p><strong>Postcode:</strong><br />
<input type="text" name="f_name" value="<? echo "$postcode"; ?>" size="35" maxlength="75" /></p>
<p><strong>Country:</strong><br />
<input type="text" name="f_name" value="<? echo "$country"; ?>" size="35" maxlength="75" /> </p>
<p><strong>First Name:</strong><br />
<input type="text" name="f_name" value="<? echo "$f_name"; ?>" size="35" maxlength="75" /></p>
</td>
<td align="top">
<p><strong>Prim Tel:</strong><br />
<input type="text" name="f_name" value="<? echo "$prim_tel"; ?>" size="35" maxlength="75" /></p>
<p><strong>Sec Tel:</strong><br />
<input type="text" name="f_name" value="<? echo "$sec_tel"; ?>" size="35" maxlength="75" /></p>
<p><strong>Email:</strong><br />
<input type="text" name="f_name" value="<? echo "$email;" ?>" size="35" maxlength="75" /> </p>
<p><strong>Birthday:</strong><br />
<input type="text" name="f_name" value="<? echo "$birthday"; ?>" size="35" maxlength="75" /> </p>
</td>
</tr>
<tr>
<td align="center">
<p><input type="submit" name="submit" value="Update Contact" /></p>
<br />
<p>Retuen To Menu</p>
</td>
</tr>
</table>
</form>
</body>
</html>
note for site admin, I am re posting this question with the hope of someone else reading over it. older questions seem to go dead after a while.
I think you're missing an "=" sign on the value attribute here:
$option_block .= "<option value\"$id\">$f_name, $l_name</option>";
Maybe you meant this?
$option_block .= "<option value=\"$id\">$f_name, $l_name</option>";