I have a relatively simple PHP page called editcustomers with 3 columns. The problem I'm having is that the form will show when there is a record in the database and the fields will be populated with that info.
When no such records exists, the form is not even shown, eliminating the possibility to insert a record.
My page layout is as follows:
Column 1 shows a form containing customer information, allowing it to
be edited.
Column 2 allows ordering of products and showing how many products were ordered
Column 3 shows the total paid so far, and the total owing.
The code for the page I have at present:
<html>
<?php
$id = $_GET['id'];
require_once('connect.php');
$sth = $dbh->query("SELECT * FROM users where id = '$id';");
$sth->setFetchMode(PDO::FETCH_ASSOC);
$eth = $dbh->query("SELECT * FROM purchases where id = '$id';");
$eth->setFetchMode(PDO::FETCH_ASSOC);
?>
<div id="main">
<div id="left">
<form name="custInfo" action ="process.php" method ="post" >
<input type = "hidden" name ="formType" value="custInfo"/>
<?php while($row = $sth->fetch()){ ?>
<p><input type = "hidden" name ="id" value="<?php echo $row["id"] ?>"/>
<p><input type = "text" name ="firstName" size ="30" value=" <?php echo $row["firstName"]?>"/>
<p><input type = "text" name ="lastName" size ="30" value="<?php echo $row["lastName"]?>"/>
<p><input type = "text" name ="country" size ="30" value="<?php echo $row["country"]?>"/>
<p></p>
<input type="submit" value="Update" />
<?php }?>
</div>
<div id="mid">
<form name="custCosts" action ="process.php" method ="post" >
<input type = "hidden" name ="formType" value="custCosts"/>
<?php while($row = $eth->fetch()){ ?>
<p><input type = "hidden" name ="id" value="<?php echo $row["id"] ?>"/>
<p><input type = "text" name ="amountOwed" size ="30" value=" <?php echo $row["amountOwed"]?>"/>
<p><input type = "text" name ="numAaa" size ="30" value="<?php echo $row["numAaa"]?>"/>
<p><input type = "text" name ="numBbb" size ="30" value="<?php echo $row["numBbb"]?>"/>
<p></p>
<input type="submit" value="Update" />
<?php }?>
</div>
<div id="right">
<b>Total Balance</b>
<p> Money owed: </p>
<p> aaa total: </p>
<p> bbb total: </p>
<p> Total: </p>
<input type = "text" name ="pay" size ="20" /></p>
<input type="submit" value="Make Payment" />
</div>
<?php
$dbh =null;
?>
</body>
</html>
And the code for all the database trickery:
<?php
require_once 'connect.php';
$formType = $_POST['formType'];
$id = $_POST['id'];
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$country = $_POST['country'];
$amountOwed = $_POST['amountOwed '];
$numAaa = $_POST['numAaa'];
$numBbb = $_POST['numBbb'];
if(empty($_POST['id'])) {
$sth = $dbh->prepare("INSERT INTO customers (firstName, lastName, country)
VALUES ('$firstName', '$lastName', '$country')");
$sth->execute();
} elseif(!empty($_POST['id']) && !isset($_POST['stayCost']) && $_POST['formType'] == 'guestInfo'){
$sth = $dbh->prepare("UPDATE customers SET firstName = '$firstName', lastName = '$lastName', country = '$country' WHERE id = '$id'");
$sth->execute();
}elseif(!empty($_POST['id']) && isset($_POST['stayCost']) && $_POST['formType'] == 'guestInfo'){
$sth = $dbh->prepare("INSERT INTO purchases (id, amountOwed, numAaa, numBbb)
VALUES ('$id', '$amountOwed', '$numAaa', '$numBbb'");
$sth->execute();
}elseif(!empty($_POST['id']) && $_POST['formType'] == 'guestCosts'){
$sth = $dbh->prepare("UPDATE purchases SET amountOwed= '$amountOwed', numAaa = '$numAaa', numBbb= '$numBbb' WHERE id = '$id'");
$sth->execute();
}
$dbh =null;
?>
Why does the form not even display if there is no record? An error or something I might understand....but the form is still in the HTML and should still be being output, from what I can see. Why is this not the case?
while($row = $sth->fetch())
That means: for each returned row do something. If there's no returned row, nothing included inside the while will execute, so it isn't printing any input!
You should include a if with the rowCount, if this is equal to 0 print inputs so the user can fill them.
<?php while($row = $sth->fetch()){ ?>
<p><input type = "hidden" name ="id" value="<?php echo $row["id"] ?>"/>
<p><input type = "text" name ="firstName" size ="30" value=" <?php echo $row["firstName"]?>"/>
<p><input type = "text" name ="lastName" size ="30" value="<?php echo $row["lastName"]?>"/>
<p><input type = "text" name ="country" size ="30" value="<?php echo $row["country"]?>"/>
<p></p>
<input type="submit" value="Update" />
<?php
}
if($sth->rowCount()==0){
?>
<p><input type = "hidden" name ="id" value=""/>
<p><input type = "text" name ="firstName" size ="30" value=""/>
<p><input type = "text" name ="lastName" size ="30" value=""/>
<p><input type = "text" name ="country" size ="30" value=""/>
<p></p>
<input type="submit" value="Update" />
<?php } ?>
Related
I Am trying to edit user details from a database. I have queried the database and stored the info in $row, but each time I try to echo the details I get the following error: Trying to access array offset on the value of type null in
C:\xampp\htdocs\merchant\admin\edituserhis.php
I seem not to know what I am doing wrong in the SQL statement, I have checked the Variable $id and its working well.
<div class="quotes">
<?php
$con = mysqli_connect("localhost","root","","merchant_db");
$id = $_REQUEST['username'];
$query = "SELECT * from user_trans where userid='".$id."'";
$result = mysqli_query($con, $query) or die ( mysqli_error());
$row = mysqli_fetch_assoc($result);
?>
<?php echo $row['userid']; ?>;
<form name="form" method="post" action="userprofile.php">
<input type="hidden" name="new" value="1" />
<input name="id" type="hidden" value="<?php echo $row['userid'];?>" />
<p><input type="text" name="balance" placeholder="Enter Amount" required value="<?php echo $row['description'];?>" /></p>
<p><input name="submit" type="submit" value="Credit" /></p>
</form>
</div>
I have a form that has the name value of the input element dynamically populated from a mysql query. Problem I am having is that I dont know to assign the value to a valid variable on form submission for later use, i cant declare the variable earlier in the scrip because i dont know it until the query is executed? how do i assign the name value to a $_POST variable? thanks.
my code
$con = new mysqli('localhost', 'jolly' ,'xxxx', 'jolly');
$query = 'SELECT * FROM names ORDER BY name ASC';
$result = mysqli_query($con, $query);
while ($row = mysqli_fetch_assoc($result)) {
?>
<p align="right"><?php echo $row['name'] ?>:
<input type="int" name="<?php echo $row['name'].'_score' ?>" />
</br>
<?php } ?>
<p align="right">
<input type="submit" name="submit" value="Next" />
</p>
</form>
Just use the value attribute of tag like this
<input type="text" name="somename" value="<?php echo $row['name'].'_score' ?>">
i am sure this will help
One possibility to solve your conundrum would be to run the sql query before generating the html and storing the query results in an array / object / session. Once the values are stored you can generate the html but by that stage would know the names and values from the db call.
$con = new mysqli( 'localhost', 'jolly' ,'xxxx', 'jolly' );
$query = 'select * from `names` order by `name` asc';
$result = mysqli_query( $con, $query );
$data = array();
/* add content from db to storage object */
while( $row = mysqli_fetch_assoc( $result ) ) $data[]=$row['name'];
/* possibly save as a session? */
$_SESSION['names']=$data;
/* generate the html */
<form method='post' name='names' action='/path/to/script.php' enctype='application/x-www-form-urlencoded'>
<?php
foreach( $data as $i => $name ){
echo "<p align='right'>{$name}<input type='int' name='{$name}_score' /></p>";
}
?>
<p align="right">
<input type="submit" name="submit" value="Next" />
</p>
</form>
if you stored the data in a session you should be able to access it in the form handler script if required.
You do not need to assign an explicit name to an input. What you have to is distinguish the forms. When submitting the post, the entire form is submitted.
your.php:
<?php
$var = $_post['data'];
...
>
form.php:
...
$con = new mysqli('localhost', 'jolly' ,'xxxx', 'jolly');
$query = 'SELECT * FROM names ORDER BY name ASC';
$result = mysqli_query($con, $query);
while ($row = mysqli_fetch_assoc($result)) {
?>
<form id = "form_<?php echo $row['name'].'_score' ?>" name = "form_<?php echo $row['name'].'_score' ?>" action = "your.php" method = "POST">
<p align="right"><?php echo $row['name'] ?>:
<input type="int" name="data" value= "<?php echo $row['name'].'_score' ?>" />
</br>
<?php } ?>
<p align="right">
<input type="submit" name="submit" value="Next" />
</p>
</form>
A B C
------- --------- ------------
id_A* id_B* id_C*
name id_A** name
address id_C**
I created a table as in the example above I've tried to do input in Table A, B then C, but what if I want to do the input of C then the input table B in conjunction with Table A, all tables in the auto increment primary key
<input type="text" name="id_A" />
<input type="text" name="id_B" />
<input type="text" name="name" />
<input type="text" name="address" />
<select class="form-control" name="id_C">
<?php
$query = "select * from C";
$r = mysql_query($query);
while ($row=mysql_fetch_array($r)) {
?>
<option value="<?php echo $row['id_C'];?>" name="id_C"><?php echo $row['name'];?></option>
<?php
}
?>
</select>
And Then
isset($_POST['submit'])) {
$id_A = $_POST['id_A'];
$name = $_POST['name'];
$address = $_POST['address'];
$q = "insert into A (id_A,name,address) VALUES('$id_A','$name','$address')";
$dq = mysql_query($q);
$id_B = $_POST['id_B'];
$query = mysql_query("insert into B(id_b,id_A,id_C) values('$id_b', '$id_A' ,'$id_C')");
please help me,
i' am a realy2 confuse
I have an administrator.php which displays 300 records from a table called 'player'. Next to each record, there is an edit option which redirects you to edit.php and the 15 columns of that record (including the primary key - playerid) is displayed inside text boxes. Line of code below:
<a href='edit.php?playerid=".$query2['playerid']."'>Edit</a>
On edit.php you are able to change data of these columns. Upon submit, an update query is sent to update the table but unfortunately, it's not working. My error message continues to display ("testing for error..."); not sure why.
//Setups up the database connection
$link = mysql_connect("localhost", "root", "");
mysql_select_db("fantasymock", $link);
if(isset($_GET['playerid'])) {
$playerid = $_GET['playerid'];
//Query to display results in input box
$query1 = mysql_query("SELECT * from player WHERE playerid = '$playerid'");
$query2 = mysql_fetch_array($query1);
}
if(isset($_POST['submit'])) {
$playerid = $_POST['playerid'];
$preranking = $_POST['preranking'];
$playerlast = $_POST['playerlast'];
$playerfirst = $_POST['playerfirst'];
$position = $_POST['position'];
$battingavg = $_POST['battingavg'];
$run = $_POST['run'];
$homerun = $_POST['homerun'];
$rbi = $_POST['rbi'];
$sb = $_POST['sb'];
$win = $_POST['win'];
$save = $_POST['save'];
$strikeout = $_POST['strikeout'];
$era = $_POST['era'];
$whip = $_POST['whip'];
//Query to update dB
$query3 = mysql_query("UPDATE player SET playerid='$playerid', preranking='$preranking', playerlast='$playerlast', playerfirst='$playerfirst', position='$position', battingavg='$battingavg', run='$run', homerun='$homerun', rbi='$rbi', sb='$sb', win='$win', save='$save', strikeout='$strikeout', era='$era', whip='$whip' WHERE playerid='$playerid'");
header("Location: administrator.php");
} else {
echo "Testing For Error....";
}
?>
<form action="" method="POST">
Player ID:<input type="text" name="playerid" value="<?php echo $query2['playerid'];?>"/> <br/>
Preranking:<input type="text" name="preranking" value="<?php echo $query2['preranking'];?>"/> <br/>
Last Name:<input type="text" name="playerlast" value="<?php echo $query2['playerlast'];?>"/> <br/>
First Name:<input type="text" name="playerfirst" value="<?php echo $query2['playerfirst'];?>"/> <br/>
Position:<input type="text" name="position" value="<?php echo $query2['position'];?>"/> <br/>
Batting Avg:<input type="text" name="battingavg" value="<?php echo $query2['battingavg'];?>"/> <br/>
Runs:<input type="text" name="run" value="<?php echo $query2['run'];?>"/> <br/>
Homeruns:<input type="text" name="homerun" value="<?php echo $query2['homerun'];?>"/> <br/>
Rbi:<input type="text" name="rbi" value="<?php echo $query2['rbi'];?>"/> <br/>
Sb:<input type="text" name="sb" value="<?php echo $query2['sb'];?>"/> <br/>
Wins:<input type="text" name="win" value="<?php echo $query2['win'];?>"/> <br/>
Saves:<input type="text" name="save" value="<?php echo $query2['save'];?>"/> <br/>
Strikeouts:<input type="text" name="strikeout" value="<?php echo $query2['strikeout'];?>"/> <br/>
Era:<input type="text" name="era" value="<?php echo $query2['era'];?>"/> <br/>
Whip:<input type="text" name="whip" value="<?php echo $query2['whip'];?>"/> <br/>
<br>
<input type="submit" name="submit" value="submit">
</form>
FYI: Every column in the table and tablename is spelled correctly, I've triple checked before posting. And I'm aware of MySQL injection. Can someone see a problem? Thank you in advance!
EDIT: I just added an additional if statement if($query3) and it now works.
You are checking for POST variables, but you are getting to edit.php through a GET request. There isn't anything on $_POST. Therefore it drops down to the else of your if block and prints out Testing For Error...
Your script in getting into the else part. That means there nothing it is getting as $_POST['submit']. Make sure that your submit button must have a name attribute as submit.
<input type="submit" name="submit" value="" />
please check what showing in error.log file. You may insert these lines at your edit.php file
error_reporting(E_ALL);
ini_set('display_errors', 1);
to display error.
Replace your else part by this for more detailed mysql errors
else{ echo "Testing For Error...." .mysql_error(); }
I am trying to create a form that will edit rows from my db table. (Based on some code I got from a StackOverflow page.)
I am able to populate the form with relevant data, but when I submit the form, the row isn't updated. In fact, some of my columns are deleted.
What did I do wrong?
edit.php
<?php
$UID = (int)$_GET['f'];
$query = mysql_query("SELECT * FROM user_feeds WHERE feed_id = '$UID'") or die(mysql_error());
if(mysql_num_rows($query)>=1){
while($row = mysql_fetch_array($query)) {
$feedtitle = $row['feed_title'];
$feedurl = $row['feed_url'];
$feedorder = $row['feed_order'];
$feedowner = $row['feed_owner'];
}
?>
<form action="update.php" method="post">
<input type="hidden" name="ID" value="<?=$UID;?>">
Title:<br /> <input type="text" name="ud_feedtitle" value="<?=$feedtitle?>"><br>
URL: <br /> <input type="text" name="ud_feedurl" value="<?=$feedurl?>"><br>
Order: <br /> <input type="text" name="ud_feedorder" value="<?=$feedorder?>"><br>
Owner:<br /> <input type="text" name="ud_feedowner" value="<?=$feedowner;?>"><br>
<input type="Submit">
</form>
<?php
}else{
echo 'No entry found. Go back';
}
?>
</div>
</body>
</html>
update.php
<?php
$ud_ID = $_REQUEST["ID"];
$ud_feedtitle = $_POST["feed_title"];
$ud_feedurl = $_POST["feed_url"];
$ud_feedorder = $_POST["feed_order"];
$ud_feedowner = $_POST["feed_owner"];
$query = "UPDATE user_feeds SET feed_title = '$ud_feedtitle', feed_url = '$ud_feedurl', feed_order = '$ud_feedorder', feed_owner = '$ud_feedowner', WHERE feed_id = '$ud_ID'";
$res = mysql_query($query);
if ($res)
echo "<p>Record Updated<p>";
else
echo "Problem updating record. MySQL Error: " . mysql_error();
?>
Reason:
The name of the input field is the same name by which $_POST is populated. The variables you are currently requesting :
$_POST["feed_title"];, $_POST["feed_url"];, $_POST["feed_order"];, $_POST["feed_owner"];
are all empty as they don't exist. When updating, you are replacing the values in your table with blank values.
Solution:
In your update.php, the following should be there instead.
$ud_ID = $_POST["ID"];
$ud_feedtitle = $_POST["ud_feedtitle"]; //corresponding to <input type="text" name="ud_feedtitle" ...
$ud_feedurl = $_POST["ud_feedurl"]; //corresponding to <input type="text" name="ud_feedurl" ...
$ud_feedorder = $_POST["ud_feedorder"]; //corresponding to <input type="text" name="ud_feedorder" ...
$ud_feedowner = $_POST["ud_feedowner"]; //corresponding to <input type="text" name="ud_feedowner" ...