I am still fairly new to PHP. My goal here is to have a fully functional form that will validate and send the data to a database. I don’t need help with the validation part (yet anyway). I am trying to have data from the form displayed so the user can update the content or delete it from the database.
Here’s the form:
[php]
Address Book .error {color: #FF0000;}* required field.
First Name: <input type="text" name="firstname"><br />
Last Name: <input type="text" name="lastname"><br />
Gender: <input type="radio" name="gender" checked value="Male">Male
<input type="radio" name="gender" value="Female"> Female<br />
E-Mail: <input type="text" name="email"><br />
Web Address: <input type="text" name="webaddress"><br /><br />
<input type="submit" id="submit" value="Submit">
<?php
if (isset($_POST['submit'])) {
$con=mysqli_connect("$localhost","$username","$password","$database");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// escape variables for security
$firstname = mysqli_real_escape_string($con, $_POST['firstname']);
$lastname = mysqli_real_escape_string($con, $_POST['lastname']);
$gender = mysqli_real_escape_string($con, $_POST['gender']);
$email = mysqli_real_escape_string($con, $_POST['email']);
$webaddress = mysqli_real_escape_string($con, $_POST['webaddress']);
$sql="INSERT INTO friends (FirstName, LastName, Gender, Email, Webaddress)
VALUES ('$firstname', '$lastname', '$gender', '$email', '$webaddress')";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
}
?>
[/php]
and Here is the FormActions.php
[php]
<?php $con=mysqli_connect("$localhost","$username","$password","$database"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } if(isset($_POST['update'])){ $UpdateQuery = "UPDATE friends SET Firstname='$_POST[firstname]', Lastname='$_POST[lastname]', Gender='$_POST[gender]', Email='[email]', Webaddress='[webaddress]' WHERE Webaddress='$_POST[hidden]'"; mysqli_query($con,$UpdateQuery); }; if(isset($_POST['delete'])){ $DeleteQuery = "DELETE FROM friends WHERE Webaddress='$_POST[hidden]'"; mysqli_query($con,$DeleteQuery); }; $sql = "SELECT * FROM friends"; $myData = mysqli_query($con,$sql); echo "First Name | Last Name | Sex | Web Address | ||||
---|---|---|---|---|---|---|---|
" . "" . " | "; echo "" . "" . " | "; echo "" . "" . " | "; echo "" . "" . " | "; echo "" . "" . " | "; echo "" . "" . " | "; echo "" . "<input type=submit name=update value=update" . " | "; echo "" . "<input type=submit name=delete value=delete" . " | "; echo "
I know I am overlooking something. Right now the form is not submitting or updating to the database. It was submitting an hour ago - time to step away for a moment. As of the time of this post I am able to delete from the database from the form. Any help would be appreciated. If you made it this far in this post I owe you a sincere thanks for reading through thus far.