I am currently trying to write a system where I can add editable fields from the database onto a webpage then submit the information and change the data that's in the database.
I have got the data to be shown in the text fields but I cannot seem to get the update function to work...
<?php $mysqli = new mysqli('localhost', 'adwawdaw', 'awdawdaw', 'awdawdaw');
$result = $mysqli->query('SELECT * FROM information WHERE id = 1');
if (is_object($result)) {
if ($result->num_rows) {
while ($row = $result->fetch_assoc()) {
$banner = $row['banner'];
$embed = $row['embed'];
$title = $row['title'];
$date = $row['date'];
$time = $row['time'];
$description = $row['description'];
$region = $row['region'];
$sponsors = $row['sponsors'];
}
}
}
?>
<form method="post" action="edit1.php">
<table>
<br />
<tr>
<td>Banner: </td>
<td><input type="text" name="banner" value="<?php echo htmlspecialchars($banner) ?>"></td>
</tr>
<tr>
<td>Embed: </td>
<td><input type="text" name="embed" value="<?php echo htmlspecialchars($embed) ?>"></td>
</tr>
<tr>
<td>Event Title: </td>
<td><input type="text" name="title" value="<?php echo htmlspecialchars($title) ?>"></td>
</tr>
<tr>
<td>Date: </td>
<td><input type="text" name="date" value="<?php echo htmlspecialchars($date) ?>"></td>
</tr>
<tr>
<td>Time: </td>
<td><input type="text" name="time" value="<?php echo htmlspecialchars($time) ?>"></td>
</tr>
<tr>
<td>Description: </td>
<td><input type="text" name="description" value="<?php echo htmlspecialchars($description) ?>"></td>
</tr>
<tr>
<td>Region: </td>
<td><input type="text" name="region" value="<?php echo htmlspecialchars($region) ?>"></td>
</tr>
<tr>
<td>Sponsors: </td>
<td><input type="text" name="sponsors" value="<?php echo htmlspecialchars($sponsors) ?>"></td>
</tr>
</table>
<input class="button" type="submit" value="Update!">
</form>
<?php
$banner = $_POST['banner'];
$embed = $row['embed'];
$title = $_POST['title'];
$date = $row['date'];
$time = $row['time'];
$description = $row['description'];
$region = $row['region'];
$sponsors = $row['sponsors'];
$mysqli->query('UPDATE livestreamadmin SET title = $title WHERE id=1');
?>
Related
I have a table of products (data pulled from SQL database) with this fields: ID, Name, Price.
After i pulled the data i have a QTY cell for each product...
I want to make a cell at the bottom to sum all the qty*prodcut price to get total price of the order...
That's my table:
<table>
<tr>
<td>Name</td>
<td>Price</td>
<td>Qty</td>
</tr>
<form action="sendorder.php?supid=<?php echo $supid; ?>" method="post">
<?php
while($row = mysqli_fetch_array($result)) {
$prodname = $row['name'];
$supid = $row['supplier'];
$prodid = $row['id'];
$prodprice = $row['price'];
?>
<tr>
<td>
<input type="checkbox" name="prod[]" id="prod[<?php echo $prodid; ?>]" value="<?php echo $prodid; ?>">
<label for="prod[<?php echo $prodid; ?>]"><?php echo $prodname; ?></label>
</td>
<td>
<?php echo number_format($prodprice, 2); ?><label for="prod[<?php echo $prodid; ?>]"> NIS</label>
</td>
<td>
<label for="prod[<?php echo $prodid; ?>]"><input style="font-size: 12px;" type="text" name="qty_<?php echo $prodid; ?>" placeholder="Qty" minlength="1" maxlength="3" size="2"></label>
</td>
</tr>
<?php
}
?>
<tr>
<td><b>Total Price:</b></td>
<td rowspan="1" colspan="2">XXX NIS</td> \\ HERE I WANT TO MAKE THE LIVE CALCLUTION
</tr>
<tr>
<td rowspan="1" colspan="3">
<input type="submit" name="submit" value="Submit">
</td>
</tr>
</form>
tnx for everyone, i found this LINK
and that's excatly what i was looking for.
function getTotal(){
var total = 0;
$('.sum').each(function(){
total += parseFloat(this.innerHTML)
});
$('#total').text(total);
}
getTotal();
$('.qty').keyup(function(){
var parent = $(this).parents('tr');
var price = $('.price', parent);
var sum = $('.sum', parent);
var value = parseInt(this.value) * parseFloat(price.get(0).innerHTML||0);
sum.text(value);
getTotal();
})
add $sum = 0; variable before while and put $sum += $row['price']; inside your while you can access $sum in Total Price <td>.
maybe you need int cast for $row['price'].
<table>
<tr>
<td>Name</td>
<td>Price</td>
<td>Qty</td>
</tr>
<form action="sendorder.php?supid=<?php echo $supid; ?>" method="post">
<?php
$sum = 0;
while($row = mysqli_fetch_array($result)) {
$prodname = $row['name'];
$supid = $row['supplier'];
$prodid = $row['id'];
$prodprice = $row['price'];
$sum += $row['price'];
?>
<tr>
<td>
<input type="checkbox" name="prod[]" id="prod[<?php echo $prodid; ?>]" value="<?php echo $prodid; ?>">
<label for="prod[<?php echo $prodid; ?>]"><?php echo $prodname; ?></label>
</td>
<td>
<?php echo number_format($prodprice, 2); ?><label for="prod[<?php echo $prodid; ?>]"> NIS</label>
</td>
<td>
<label for="prod[<?php echo $prodid; ?>]"><input style="font-size: 12px;" type="text" name="qty_<?php echo $prodid; ?>" placeholder="Qty" minlength="1" maxlength="3" size="2"></label>
</td>
</tr>
<tr>
<td><b>Total Price:</b></td>
<td rowspan="1" colspan="2"><?php echo $sum; ?></td> \\ HERE I WANT TO MAKE THE LIVE CALCLUTION
</tr>
<?php
}
?>
<tr>
<td rowspan="1" colspan="3">
<input type="submit" name="submit" value="Submit">
</td>
</tr>
</form>
I have an HTML table which is getting data from MySql.
existingbankproducts table is below:
I have 2 calculated fields which need to sum up the numbers that I get. The fields that are needed to calculate is Balance and MonthlyCommitment.
The code I use to get the result from MySql is below:
<?php
$stmt2 = $DB_con->prepare("SELECT * FROM applicantpersonaldetails apd "
. "INNER JOIN existingbankproducts ext ON apd.ApplicantID = ext.ApplicantID "
. "WHERE apd.AccountID =:accountId AND apd.applicantType ='main';");
$stmt2->bindParam(':accountId', $accountId, PDO::PARAM_INT);
//if account id data type is varchar change the last parameter to `PDO::PARAM_str`
$stmt2->execute();
if ($stmt2->rowCount() > 0) {
?>
<table>
<tr>
<th>Financial Institution</th>
<th>Product Type</th>
<th>Balance</th>
<th>Monthly Commitment</th>
</tr>
<?php
while ($row = $stmt2->fetch(PDO::FETCH_ASSOC)) {
?>
<tr>
<td><input type = "text" name = "finanIns1" id = "finanIns1" value="<?php echo $row['FinanicalInstituion']; ?>" readonly></td>
<td>
<input list = "proTypeList" name = "proType1" id = "proType1" value="<?php echo $row['ProductType']; ?>"readonly >
</td>
<td id = "balance"><input type = "number" name = "balance1" id = "balance1" value="<?php echo $row['Balance']; ?>"readonly></td>
<td id = "MonthyComm"><input type = "number" name = "monthlyComm1" id = "monthlyComm1" value="<?php echo $row['MonthlyCommitment']; ?>"readonly></td>
</tr>
<?php
}
} else {
?>
<div class="">
<div class="alert alert-warning">
<span class="glyphicon glyphicon-info-sign"></span> No Data Found ...
</div>
</div>
<?php
}
?>
<?php
while ($row = $stmt2->fetch(PDO::FETCH_ASSOC)) {
$totalBalance = 0;
$totalBalance += $row['Balance'];
$totalMonthlyComm = 0;
$totalMonthlyComm +=$row['MonthlyCommitment'];
?>
<tr>
<td>Total:</td>
<td><input readonly></td>
<td id="balance"><input type="number" name="totalBalance" id="totalBalance" value="<?php echo $totalBalance; ?>" min="0" readonly></td>
<td id="MonthyComm"><input type="number" name="totalMonthlyComm" id="totalMonthlyComm" value="<?php echo $totalMonthlyComm; ?>" min="0" readonly></td>
</tr>
<?php
}
?>
</table>
I am able to retrieve the data from MySql. However, it is not able to sum up the totalBalance and totalMonthlyComm at the end of my table.
You only need to loop through the database result only once:
<?php
$stmt2 = $DB_con->prepare("SELECT * FROM applicantpersonaldetails apd "
. "INNER JOIN existingbankproducts ext ON apd.ApplicantID = ext.ApplicantID "
. "WHERE apd.AccountID =:accountId AND apd.applicantType ='main';");
$stmt2->bindParam(':accountId', $accountId, PDO::PARAM_INT);
//if account id data type is varchar change the last parameter to `PDO::PARAM_str`
$stmt2->execute();
$totalBalance = 0;
$totalMonthlyComm = 0;
if ($stmt2->rowCount() > 0) {
?>
<table>
<tr>
<th>Financial Institution</th>
<th>Product Type</th>
<th>Balance</th>
<th>Monthly Commitment</th>
</tr>
<?php
while ($row = $stmt2->fetch(PDO::FETCH_ASSOC)) {
$totalBalance += $row['Balance'];
$totalMonthlyComm +=$row['MonthlyCommitment'];
?>
<tr>
<td><input type="text" name="finanIns1" id = "finanIns1" value="<?php echo $row['FinanicalInstituion']; ?>" readonly></td>
<td><input list="proTypeList" name="proType1" id="proType1" value="<?php echo $row['ProductType']; ?>" readonly></td>
<td id="balance"><input type="number" name="balance1" id="balance1" value="<?php echo $row['Balance']; ?>" readonly></td>
<td id="MonthyComm"><input type="number" name="monthlyComm1" id="monthlyComm1" value="<?php echo $row['MonthlyCommitment']; ?>" readonly></td>
</tr>
<?php
}
?><tr>
<td>Total:</td>
<td><input readonly></td>
<td id="balance"><input type="number" name="totalBalance" id="totalBalance" value="<?php echo $totalBalance; ?>" min="0" readonly></td>
<td id="MonthyComm"><input type="number" name="totalMonthlyComm" id="totalMonthlyComm" value="<?php echo $totalMonthlyComm; ?>" min="0" readonly></td>
</tr>
</table>
<?php
} else {
?>
<div class="">
<div class="alert alert-warning">
<span class="glyphicon glyphicon-info-sign"></span> No Data Found ...
</div>
</div>
<?php
} ?>
Change your lines from while as :
<?php
$totalBalance = 0;
$totalMonthlyComm = 0;
while ($row = $stmt2->fetch(PDO::FETCH_ASSOC)) {
$totalBalance += $row['Balance'];
$totalMonthlyComm +=$row['MonthlyCommitment'];
}
?>
<tr>
<td>Total:</td>
<td><input readonly></td>
<td id="balance"><input type="number" name="totalBalance" id="totalBalance" value="<?php echo $totalBalance; ?>" min="0" readonly></td>
<td id="MonthyComm"><input type="number" name="totalMonthlyComm" id="totalMonthlyComm" value="<?php echo $totalMonthlyComm; ?>" min="0" readonly></td>
</tr>
I am not getting my orignal array after using unserialize().
I am retrieving array from database and want to display it.
I have 2 arrays, created them from checkbox input, then I serialize() them and stored it in database.
Create.php
<table>
<tr>
<td>Aadhar</td>
<td><input type="checkbox" name="original[]" value="Aadhar"/></td>
<td><input type="checkbox" name="xerox[]" value="Aadhar"/></td>
</tr>
<tr>
<td>Pan Card</td>
<td><input type="checkbox" name="original[]" value="Pan Card"/></td>
<td><input type="checkbox" name="xerox[]" value="Pan Card"/></td>
</tr>
<tr>
<td>Address</td>
<td><input type="checkbox" name="original[]" value="Address"/></td>
<td><input type="checkbox" name="xerox[]" value="Address"/></td>
</tr>
<tr>
<td>Light Bill</td>
<td><input type="checkbox" name="original[]" value="Light Bill"/></td>
<td><input type="checkbox" name="xerox[]" value="Light Bill"/></td>
</tr>
<tr>
<tr>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
Create2.php(this page is just like confirmation page..before user submit to the database.)
<?php
$name = $_POST['name'];
$mobile = $_POST['mobile'];
$email = $_POST['email'];
$company = $_POST['company'];
$original_list = $_POST['original'];
$xerox_list = $_POST['xerox'];
?>
<form action="create2.php" method="POST">
<table cellpadding="10">
<tr>
<td>Name</td>
<td><input type="text" name="name" value="<?php echo $name; ?>" readonly="readonly" ></td>
</tr>
<tr>
<td>Mobile</td>
<td><input type="text" name="mobile" value="<?php echo $mobile; ?>" readonly="readonly" /></td>
</tr>
<tr>
<td>Email</td>
<td><input type="text" name="email" value="<?php echo $email; ?>" readonly="readonly" /></td>
</tr>
<tr>
<td>Company</td>
<td><input type="text" name="company" value="<?php echo $company; ?>" readonly="readonly" /></td>
</tr>
<tr>
<input type="hidden" name="original_list" value="<?php $original_list ?>" />
<input type="hidden" name="xerox_list" value="<?php $xerox_list ?>" />
<td>Documents Selected</td>
<td>
<?php
echo "<b>ORIGINAL</b><br />";
foreach($original_list as $value)
{
echo $value."<br />";
}
?>
</td>
<td>
<?php
echo "<b>XEROX</b><br />";
foreach($xerox_list as $value)
{
echo $value."<br />";
}
?>
</td>
</tr>
<tr>
<td><input type="submit" value="Confirm" /></td>
</tr>
</table>
</form>
</body>
Create2.php (insert in database)
$name = $_POST['name'];
$mobile = $_POST['mobile'];
$email = $_POST['email'];
$company = $_POST['company'];
$original = serialize($_POST['original_list']);
$xerox = serialize($_POST['xerox_list']);
echo $sql = "INSERT INTO users (name,mobile,email,company,original,xerox)
VALUES ('$name','$mobile','$email','$company','$original','$xerox')";
mysqli_query($con,$sql);
So when I try to unserialize, and display the array using foreach, this is what I get:
Warning: Invalid argument supplied for foreach()
Code:
$original = unserialize($rows['original']);
foreach($original as $value)
{
echo $value."<br />";
}
As a customer, I have no problem in displaying my details/profile but I can't update/edit my profile even though I have clicked the "Save" button. Am I missing something here?
edit_customer_profile.php
<?php
header('Content-Type: text/html; charset=utf-8');
include 'connection.php';
session_start();
if(isset($_SESSION['CustomerID'])) {
$customerID = $_SESSION['CustomerID'];
$customername = $_SESSION['CustomerName'];
$customers = mysql_query("select * from customer where CustomerID='".$customerID."'");
$customer = mysql_num_rows($customers);
if($customer== 1){
$row = mysql_fetch_assoc($customers);
$email = $row['CustomerEmail'];
$contactno = $row['CustomerContactNo'];
$class = $row['CustomerClass'];
$campus = $row['CustomerCampus'];
$intake = $row['CustomerIntake'];
if(isset($_POST['submit'])){
$_var1 = $_POST['new_name'];
$_var2 = $_POST['new_email'];
$_var3 = $_POST['new_contactno'];
$_var4 = $_POST['new_campus'];
$_var5 = $_POST['new_intake'];
$_var6 = $_POST['new_class'];
$query1 = "UPDATE customer
SET CustomerName='$_var1', CustomerEmail='$_var2', CustomerContactNo='$_var3', CustomerCampus='$_var4', CustomerIntake='$_var5', CustomerClass='$_var6'
WHERE CustomerID='$customerID'";
}
}
}
?>
Below is the form
<form method = "post" action=">
<tr>
<td width="170">Name:</td>
<td><input type="text" name="new_name" size="30" value="<?php echo $customername ?>" /></td>
</tr>
<tr>
<td>Email:</td>
<td><input type="text" name="new_email" size="30" value="<?php echo $email ?>" /></td>
</tr>
<tr>
<td>Contact No:</td>
<td><input type="text" name="new_contactno" size="30" value="<?php echo $contactno ?>" /></td>
</tr>
<tr>
<td>Campus:</td>
<td><input type="text" name="new_campus" size="30" value="<?php echo $campus ?>" /></td>
</tr>
<tr>
<td>Intake:</td>
<td><input type="text" name="new_intake" size="30" value="<?php echo $intake ?>" /></td>
</tr>
<tr>
<td>Class:</td>
<td><input type="text" name="new_class" size="30" value="<?php echo $class ?>" /></td>
</tr>
<tr>
<td align="right"><input type="submit" size="30" name="submit" value="Save" /></td>
</tr>
</form>
You haven't run the update query.
Run your update query with
$query1 = "UPDATE customer
SET
CustomerName='$_var1',
CustomerEmail='$_var2',
CustomerContactNo='$_var3',
CustomerCampus='$_var4',
CustomerIntake='$_var5', CustomerClass='$_var6'
WHERE CustomerID='$customerID'";
mysql_query($query1);
Note: Use mysqli_* or pdo_* functions instead of mysql_ functions, which is going to deprecated.
I have created register form using php.
I want to display, current registered user details in new window.
Here is my data.php:
<?php
include('config.php');
$result = $db->prepare("SELECT * FROM crop ORDER BY id DESC");
$result->execute();
while( $row = $result->fetch()){
$fname = $row['fname'];
$lname = $row['lname'];
$email = $row['email'];
$pass = $row['pass'];
$phone = $row['phone'];
$sex_select = $row['sex_select'];
$month = $row['month'];
$day = $row['day'];
$year = $row['year'];
$id = $row['id'];
}
?>
<html>
<body>
<from >
<table style="color:purple;border-style:groove; height:150px;width:350px" background="3.jpg">
<tr>
<td style="font-family:Copperplate Gothic Bold"> </td>
</tr>
<tr>
<td style="color:red;background-color:aqua;" class="auto-style3">Id no:</td>
<td class="auto-style4">
<input id="Text1" type="text" value='<?php echo $id; ?>'/></td>
</tr>
<tr>
<td style="color:red;background-color:aqua;" class="auto-style3">FirstName:</td>
<td class="auto-style4">
<input id="Text2" type="text" value='<?php echo $fname; ?>'/></td>
</tr>
<tr>
<td style="color:red;background-color:aqua;" class="auto-style3">LastName:</td>
<td class="auto-style4">
<input id="Text3" type="text" value='<?php echo $lname; ?>' /></td>
</tr>
<tr>
<td style="color:red;background-color:aqua;" class="auto-style3">Email:</td>
<td class="auto-style4">
<input id="Text4" type="text" value='<?php echo $email; ?>' /></td>
</tr>
<tr>
<td style="color:red;background-color:aqua;" class="auto-style3">Password:</td>
<td class="auto-style4">
<input id="Text5" type="text" value='<?php echo $pass; ?>' /></td>
</tr>
<tr>
<td style="color:red;background-color:aqua;" class="auto-style3">Contact Number:</td>
<td class="auto-style4">
<input id="Text6" type="text" value='<?php echo $phone; ?>' ></td>
</tr>
<tr>
<td style="color:red;background-color:aqua;" class="auto-style3">Sex_Select:</td>
<td class="auto-style4">
<input id="Text7" type="text" value='<?php echo $sex_select; ?>'/></td>
</tr>
<tr>
<td style="color:red;background-color:aqua;" class="auto-style3">Month:</td>
<td class="auto-style4">
<input id="Text8" type="text" value='<?php echo $month; ?>' /></td>
</tr>
<tr>
<td style="color:red;background-color:aqua;" class="auto-style3">Day:</td>
<td class="auto-style4">
<input id="Text9" type="text" value='<?php echo $day; ?>'/></td>
</tr>
<tr>
<td style="color:red;background-color:aqua;" class="auto-style3">Year:</td>
<td class="auto-style4">
<input id="Text10" type="text" value='<?php echo $year; ?>'/></td>
</tr>
<tr>
<td></td>
</tr>
</table>
</form>
</body>
</html>
And i include data.php file in index.php.
Now it shows, http://s28.postimg.org/ndbnjlyz1/Untitled_1_copy.png
Can anyone help me with us. Thanks in advance.
this could be done the moment you insert in to your database
//use
session_start(); //then
$v1="'" . $conn->real_escape_string('col1_value') . "'";
$sql="INSERT INTO tbl (col1_varchar, col2_number) VALUES ($v1,10)";
if($conn->query($sql) === false) {
trigger_error('Wrong SQL: ' . $sql . ' Error: ' . $conn->error, E_USER_ERROR);
}
else {
$_SESSION['user_id'] = $conn->insert_id; //get the id in a session variable
}
//redirect to another page say user_details.php and show the result using the session...
header("location:user_details.php");
make a edit in the query
$result = $db->prepare("SELECT * FROM crop
WHERE id = ".$_SESSION['user_id']."
ORDER BY id DESC"); //this brings the data of the last entered user.