'Post Form Data To phpmyadmin Database
I need to post data from a website (HTML5 based) to a database on PHPMyAdmin. I have the database created as sewcute
and a table named members
. I'm using XAMMP if that matters.
In this code, I want to post data from a registration form to the member
database.
The form:
<form id="maintext" action="scdb.php" method="post">
<fieldset id="specialRequest">
<legend>Registration Information</legend>
<label>
<input id="nameinput" name="UserName" placeholder="User Name" type="text">Create User Name
</label>
<label>
<input id="password" name="password" placeholder="Password" type="text">Password
</label>
<label for="nameinput">
<input id="nameinput" name="FirstName" placeholder="First Name" type="text">First Name
</label>
<label for="nameinput">
<input id="nameinput" name="LastName" placeholder="Last Name" type="text">Last Name
</label><label>
<input id="addrinput" name="address" placeholder="Your Address" type="text">Street Address
</label> <label>
<input id="zipinput" name="zip" placeholder="12345" type="text">Zip Code
</label>
<label>
<input id="emailinput" name="email" placeholder="[email protected]" type="email"> Email
</label>
<label>
<input id="phoneinput" name="phone" placeholder="(123)456-7890" type="text">Phone
</label>
<label><input id="submit" type="submit" value="Submit" /></label>
</fieldset>
</form>
The PHP:
<?php
$host = "localhost";
$username = "root";
$password = "";
$database = "sewcute";
$dsn = ("mysql:host=$localhost;dbname=$sewcute");
TRY {
$conn = new PDO( $dsn, $username, $password );
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
if (isset($_POST['submit'])) {
$username1 = $_POST['username1'];
$password1 = $_POST['password1'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
if (isset($_POST['UserID'])) {
$id = $_POST['UserID'];
$sql = "UPDATE members SET"
. "UserName=".$conn->quote($username1)
. "password=".$conn->quote($password1)
. "FirstName=".$conn->quote($firstname)
. "LastName".$conn->quote($lastname)
. " WHERE UserID = ".$conn->quote($id);
$members = $conn->query($sql);
} else {
$sql = "INSERT INTO members("
. "UserName, password, FirstName, LastName"
. " ) VALUES ("
. $conn->quote($username1).","
. $conn->quote($password1).","
. $conn->quote($firstname).","
. $conn->quote($lastname).")";
$members = $conn->query($sql);
}
} elseif (isset($_GET['ID'])) {
$userEditDataRows = $conn->query('SELECT * FROM members WHERE UserID ='.$conn- >quote($_GET['UserID']));
if (sizeof($userEditDataRows)>0) {
$row = $userEditDataRows[0];
$username1 = $row['username1'];
$password1 = $row['password'];
$firstname = $row['firstname'];
$lastname = $row['lastname'];
$id = $_GET['UserID'];
}
}
$table .= '</table>';
} catch (PDOException $e) {
exit("Connection failed: " . $e->getMessage());
}
?>
And here is a picture of what the table structure looks like:
I have all the website files in a folder that's on XAMPP. When I submit data from the form, I get a blank screen and nothing happens. I go to the database but nothing has been updated. Can anyone point me in the right direction? By the way, this is not a production website, it's just for class.
Solution 1:[1]
You seem to be using a variable for your DSN database name that you didn't declare:
$database = "sewcute";
$dsn = ("mysql:host=$localhost;dbname=$sewcute");
Presumably, you meant to use $database
there, or just the string "sewcute".
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | OMi Shah |