Can't write data to database - php

I am trying to write a simple application with PHP to write and receive data from a database. I am able to retrieve data already in the database through my PHP script, but I can not write anything to it. When I try, I do not get any errors - just the title of my script and a blank screen.
My code is as follows:
My HTML
<form action="insert_book.php" method="post">
<fieldset>
<p><label for="ISBN">ISBN</label>
<input type="text" id="ISBN" name="ISBN" maxlength="13" size="13" /></p>
<p><label for="Author">Author</label>
<input type="text" id="Author" name="Author" maxlength="30" size="30" /></p>
<p><label for="Title">Title</label>
<input type="text" id="Title" name="Title" maxlength="60" size="30" /></p>
<p><label for="Price">Price</label>
$ <input type="text" id="Price" name="Price" maxlength="7" size="7" /></p>
</fieldset>
<p><input type="submit" value="Add New Book" /></p>
</form>
PHP
<?php
if(!isset($_POST['ISBN']) || !isset($_POST['Author'])
|| !isset($_POST['Title']) || !isset($_POST['Price'])) {
echo "<p>You have not entered all the details.<br/>
Go back and try again.</p>";
exit;
}
// create short variable names
$isbn=$_POST['ISBN'];
$author=$_POST['Author'];
$title=$_POST['Title'];
$price=$_POST['Price'];
$price = doubleval($price);
#$db = new mysqli('localhost', 'jay', '******', 'Books');
if (msqli_connect_errno()) {
echo '<p>Error: Could not connect to database.<br/>
Please try again.</p>';
exit;
}
$query = "INSERT INTO Books VALUES (?, ?, ?, ?)";
$stmt = $db->prepare($query);
$stmt->bind_param('sssd', $isbn, $author, $title, $price);
$stmt->execute();
if ($stmt->affected_rows > 0) {
echo '<p>Submission successful.</p>';
} else {
echo '<p>An Aweful error occured.<br/>
Nothing was added.</p>';
}
$db->close();
?>
Please let me know if you can see any issues.
Thanks!

You have an error in this line:
if (msqli_connect_errno()) {
It should be
if (mysqli_connect_errno()) {
You missed a y.

Related

How to fix MySQL and PHP in WAMP not inserting data?

I’m using WAMP as my php server, I’m using PHP-AdminPage for my databse (usin MySql), Everything is working, my database is connected . except for when I want to display the data, rows are created but they’re EMPTY! even tho I didn’t leave them empty. I don’t understand, I’m so confused.
PHP Code:
<html>
<body>
<?php
$conn = new mysqli('localhost','root','');
//check connection
if ($conn-> connect_error) {
die("Connection failed: ".$conn->connect_error);
}
echo "Database Connected successfully";
//choose db
mysqli_select_db($conn,"catcafe");
//insert query
$sql="INSERT INTO customer(FName,LName,Email,PhNum,Time) VALUES('$_POST[firstname]',
'$_POST[lastname]','$_POST[Email]','$_POST[Mobile]','$_Post[Time]')";
if($conn->query($sql) === true) {
echo "New record created";
}
else{
echo "Error, check values".$sql."<br>".$conn->error;
}
mysqli_close($conn);
?>
</body>
</html>
//insert query
$sql="INSERT INTO customer(FName,LName,Email,PhNum,Time) VALUES('$_POST[firstname]',
'$_POST[lastname]','$_POST[Email]','$_POST[Mobile]','$_Post[Time]')";
MySQL keeps telling me that the error is here! but I dont know what it is, the columns have the exact same name as the ones in the database.
MY FORM /HTML CODE:
<form action="InsertReservation.php">
<label for="fname">First Name </label><br>
<input type="text" id="fname" name="firstname" placeholder="Your name.." style="width:600px; font-size: 30px;">
<br>
<label for="lname">Last Name </label><br>
<input type="text" id="lname" name="lastname" placeholder="Your last name.."style="width:600px; font-size: 30px;">
<br>
<label for="Email">Email </label><br>
<input type="text" id="Email" name="Email" placeholder="Your Email is.."style="width:600px; font-size: 30px;">
<br>
<label for="Mobile">Mobile Number </label><br>
<input type="text" id="Mobile" name="Mobile" placeholder="Your Mobile Number is.." style="width:600px; font-size: 30px;">
<br>
<label for="Time">Time </label><br>
<input type="time" id="Time" name="Time" min="8:00" required style="width:600px; font-size: 30px;">
<br>
<input type="submit" value="Submit" onclick="alert ('Thank you. Your Reservation Is Confirmed!')">
<button type="reset" value="Reset" onclick="alert ('Reset is complete')"> Reset </button> <br>
</form>
MySQL Code
SELECT * FROM `customer` ORDER BY `customer`.`FName` ASC
I want it to insert the data, but it keeps inserting empty data.
Validate your input data all the time.
Use prepared statments for user input
Time is a mysql reserved word so use backticks `` around it.
$conn = new mysqli('localhost', 'root', '');
//check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Database Connected successfully";
//choose db
mysqli_select_db($conn, "catcafe");
//insert query
$sql = "INSERT INTO customer(FName,LName,Email,PhNum,`Time`) VALUES(?,?,?,?,?)";
$stmt = $conn->prepare($sql);
$stmt->bind_param("sssss", $_POST['firstname'], $_POST['lastname'], $_POST['Email'], $_POST['Mobile'], $_POST['Time']);
if ($stmt->execute()) {
echo "New record created";
} else {
echo "Error, check values " . $stmt->error;
}
mysqli_close($conn);
?>
Hello Please use this code on a single file it's working fine.
Thanks
$conn = new mysqli('localhost', 'root', '');
//check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Database Connected successfully";
//choose db
mysqli_select_db($conn, "catcafe");
if($conn){
//insert query
if(isset($_POST['submit']) && $_POST['submit'] == 'Submit'){
$sql = "INSERT INTO customer(FName,LName,Email,PhNum,Time)
VALUES('".$_POST['firstname']."','".$_POST['lastname']."','".$_POST['Email']."','".$_POST['Mobile']."','".$_POST['Time']."')";
if($conn->query($sql) === true) {
echo "New record created";
}
else{
echo "Error, check values".$sql."<br>".$conn->error;
}
}
mysqli_close($conn);
}
<form method="post">
<label for="fname">First Name </label><br>
<input type="text" id="fname" name="firstname" placeholder="Your name.." style="width:600px; font-size: 30px;">
<br>
<label for="lname">Last Name </label><br>
<input type="text" id="lname" name="lastname" placeholder="Your last name.."style="width:600px; font-size: 30px;">
<br>
<label for="Email">Email </label><br>
<input type="text" id="Email" name="Email" placeholder="Your Email is.."style="width:600px; font-size: 30px;">
<br>
<label for="Mobile">Mobile Number </label><br>
<input type="text" id="Mobile" name="Mobile" placeholder="Your Mobile Number is.." style="width:600px; font-size: 30px;">
<br>
<label for="Time">Time </label><br>
<input type="time" id="Time" name="Time" min="8:00" style="width:600px; font-size: 30px;">
<br>
<input type="submit" name="submit" value="Submit" onclick="alert ('Thank you. Your Reservation Is Confirmed!')">
<button type="reset" value="Reset" onclick="alert ('Reset is complete')"> Reset </button> <br>
</form>

Permission denied for PHP application with xampp

I am trying to make my first php based website and have ran into a problem. The website is very basic and is intended to allow users to enter student information into a database. I built this website following a tutorial by Derek Banas which can be found here:
https://www.youtube.com/watch?v=mpQts3ezPVg&t=25s.
Whenever I try to open my getStudentInfo.php file in my browser, I receive the following error:
Warning: require_once(C:\xampp\htdocs\practiceWebDev): failed to open stream: Permission denied in C:\xampp\htdocs\practiceWebDev\practicePHPmySQL\getStudentInfo.php on line 3
Fatal error: require_once(): Failed opening required '../../practiceWebDev' (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\practiceWebDev\practicePHPmySQL\getStudentInfo.php on line 3
Does this error mean I have something wrong with my code? Or do I need to change the php.ini or httpd.conf files associated with XAMPP? Below are the files I have dealing with connections if they are of any use. Thank you very much for any feedback you all can give. Also, I make use of mysql for my database.
mysqli_connect.php:
<?php
//IMPORTANT! This file must be saved outside of where the rest of my files related to our website are saved so that no one may access them.
DEFINE ('DB_USER' 'studentweb');
DEFINE ('DB_PASSWORD' 'turtledove');
DEFINE ('DB_HOST' 'localhost');
DEFINE ('DB_NAME' 'studentdatabase');
$databaseConnection = #mysqli_connect(DB_USER, DB_PASSWORD, DB_HOST, DB_NAME)
//using the above # symbol before mysqli_connect makes it so errors will not appear in the browser.
//# is known as the 'error control operator', and makes PHP suppress error messages associated with the expression.
OR die('Could not connect to MySql lol' . mysqli_connect_error());
//mysqli_connect_error() is a function defined in the php synthax.
?>
getStudentInfo.php:
<?php
//now we require the file outside of the current directory called mysqli_connect.php
require_once('../../practiceWebDev');
//the query below will display the information from each student in the form of a table.
$query = "SELECT first_name, last_name, email, street, province,
postal_code, phone_num, birth_date, sex, date_entered,
lunch_cost, student_id, FROM students";
//the response below is all the info that we've gotten that we want to show in our table.
$response = #mysqli_query($databseConnection, $query);
//below we will see if the query executed properly.
if($response)
{
echo '<table align="left" cellspacing="5" cellpadding="8">
<tr><td align="left"><b>First Name</b></td>
<td align="left"><b>Last Name</b></td>
<td align="left"><b>Email</b></td>
<td align="left"><b>Street</b></td>
<td align="left"><b>City</b></td>
<td align="left"><b>State</b></td>
<td align="left"><b>Zip</b></td>
<td align="left"><b>Phone</b></td>
<td align="left"><b>Birth Day</b></td></tr>';
while($row = mysqli_fetch_array($response)){
echo '<tr>';
echo '<td align=left">'.$row['first_name'].'</td><td align="left">'.$row['last_name'].'</td><td align="left">'.$row['email'].'</td>';
echo '<td align="left">'.$row['street'].'</td><td align="left">'.$row['city'].'</td><td align="left">'.$row['state'].'</td>';
echo '<td align="left">'.$row['zip'].'</td><td align="left">'.$row['phone'].'</td><td align="left">'.$row['birth_date'].'</td>';
echo '</tr>';
}
echo '</table>';
}
else
{
echo "Couldn't issue database query";
echo mysqli_error($databseConnection);
}
mysqli_close($databseConnection);
?>
studentadded.php
<html>
<head>
<title>Add Student</title>
</head>
<body>
<!--First, we need to check if this page was actually reached when the form was submitted--->
<?php
//Below we check that a POST operation was completed by the button I have named "submitButton"
if(isset($_POST['submitButton']))
{
$data_missing = array();
/*If there is an empty field when a POST operation is completed, that field's name will be
added to the data_missing array so that we may visually see which fields are not being sent*/
if(empty($_POST['first_name']))
{
$data_missing[] = 'First Name';
}
else
{
$f_name = trim($POST['first_name']);
}
if(empty($_POST['lastName']))
{
$data_missing[] = 'Last Name';
}
else
{
$l_name = trim($POST['lastName']);
}
if(empty($_POST['email']))
{
$data_missing[] = 'email';
}
else
{
$email = trim($POST['email']);
}
if(empty($_POST['street']))
{
$data_missing[] = 'street';
}
else
{
$street = trim($POST['street']);
}
if(empty($_POST['province']))
{
$data_missing[] = 'province';
}
else
{
$province = trim($POST['province']);
}
if(empty($_POST['postal_code']))
{
$data_missing[] = 'postal_code';
}
else
{
$postal_code = trim($POST['postal_code']);
}
if(empty($_POST['phone_num']))
{
$data_missing[] = 'phone_num';
}
else
{
$phone_num = trim($POST['phone_num']);
}
if(empty($_POST['birth_date']))
{
$data_missing[] = 'birth_date';
}
else
{
$birth_date = trim($POST['birth_date']);
}
if(empty($_POST['sex']))
{
$sex[] = 'sex';
}
else
{
$sex = trim($POST['sex']);
}
if(empty($_POST['lunch_cost']))
{
$data_missing[] = 'lunch_cost';
}
else
{
$lunch_cost = trim($POST['lunch_cost']);
}
if(empty($_POST['student_id']))
{
$data_missing[] = 'student_id';
}
else
{
$student_id = trim($POST['student_id']);
}
//Now lets check
if(empty($data_missing))
{
require_once('../mysqli_connect.php');
$myQuery = "INSERT INTO students (first_name, last_name, email, street, province,
postal_code, phone_num, birth_date, sex, date_entered, lunch_cost,
student_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, NOW(), ?, NULL)";
$statement = mysqli_prepare($databaseConnection, $myQuery);
//We have to represent the data type for each of the value that will be passed into our database.
/*i Integers
d Doubles
b Blobs
s Everything Else*/
//Now lets bind variables to the '?'s passed in with the myQuery query.
mysqli_stmt_bind_param($statement, "sssssssisssd", $f_name, $l_name, $email, $street, $province,
$postal_code, $phone_num, $birth_date, $sex, $lunch_cost, $student_id);
mysqli_statement_execute($statement);
$affected_rows = mysqli_stmt_addected_rows($statement);
if($affected_rows == 1)
{
echo 'Went through properly! Student entered correctly!';
mysqli_stmt_close($statement);
mysqli_close($databaseConnection);
}
else
{
echo 'Error occurred :(';
echo '<br />';
echo mysqli_error();
mysqli_stmt_close($statement);
mysqli_close($databaseConnection);
}
}
else
{
echo 'You need to enter the following data my dude: <br />';
foreach($data_missing as $missingData)
{
echo "$missingData<br />";
}
}
}
?>
<form action="http://localhost/studentadded.php" method="post">
<b>Add a New Student</b>
<p>First Name: <input type="text" name="first_name" size="30" value="" /></p>
<p>Last Name: <input type="text" name="lastName" size="30" value="" /></p>
<p>Email: <input type="text" name="email" size="60" value="" /></p>
<p>Street: <input type="text" name="street" size="50" value="" /></p>
<p>Province: <input type="text" name="province" size="3" value="" /></p>
<p>Postal Code: <input type="text" name="postal_code" size="6" value="" /></p>
<p>Phone Number: <input type="text" name="phone_num" size="20" value="" /></p>
<p>Birth Date (YYYY-MM-DD): <input type="text" name="birth_date" size="20" value="" /></p>
<p>Sex: <input type="text" name="sexField1" size="5" maxlength="1" value="" />
<!---<p>Sex: <input type="radio" name="sexField" value="M" />
<br>
<input type="radio" name="sexField" value="F" checked /></p>--->
<!--<p>Date Entered: <input type="" name="" size="" value="" /></p>--->
<p>Lunch Cost: <input type="text" name="lunch_cost" size="5" value="" /></p>
<p>Student ID: <input type="text" name="student_id" size="10" value="" /></p>
<input type="submit" name="submitButton" value="submitValue">
<!-- type="submit" is a predefined term in html--->
</form>
</body>
</html>
addStudent.php
<html>
<head>
<title>Add Student</title>
</head>
<body>
<form action="http://localhost/studentadded.php" method="post">
<b>Add a New Student</b>
<p>First Name: <input type="text" name="first_name" size="30" value="" /></p>
<p>Last Name: <input type="text" name="lastName" size="30" value="" /></p>
<p>Email: <input type="text" name="email" size="60" value="" /></p>
<p>Street: <input type="text" name="street" size="50" value="" /></p>
<p>Province: <input type="text" name="province" size="3" value="" /></p>
<p>Postal Code: <input type="text" name="postal_code" size="6" value="" /></p>
<p>Phone Number: <input type="text" name="phone_num" size="20" value="" /></p>
<p>Birth Date (YYYY-MM-DD): <input type="text" name="birth_date" size="20" value="" /></p>
<p>Sex: <input type="text" name="sexField1" size="5" maxlength="1" value="" />
<!---<p>Sex: <input type="radio" name="sexField" value="M" />
<br>
<input type="radio" name="sexField" value="F" checked /></p>--->
<!--<p>Date Entered: <input type="" name="" size="" value="" /></p>--->
<p>Lunch Cost: <input type="text" name="lunch_cost" size="5" value="" /></p>
<p>Student ID: <input type="text" name="student_id" size="10" value="" /></p>
<input type="submit" name="submitButton" value="submitValue">
<!-- type="submit" is a predefined term in html--->
</form>
</body>
</html>
From what I can see in the tutorial files at this address:
http://www.newthinktank.com/2014/09/php-mysql-tutorial/
In the file getstudentinfo.php at the beginning of the file
You should have require_once('../mysqli_connect.php');
But you have require_once('../../practiceWebDev');
(there is no filename in this line of your code!)
So I think just changing this line of code and using correct path + the filename should solve your error
Update:
Again from what I can see in your code, in studentadded.php file you have this line of code:
require_once('../mysqli_connect.php');
So if your studentadded.php file is working without errors, then just change the line which is generating the error in getstudentinfo.php file with this line.

Form submit is not entering data into database

<form action="<?php $_SERVER['PHP_SELF'] ?>" method="POST">
<input type="text" name="duration" placeholder="Enter duration">
<input type="text" name="budget" placeholder="Enter Budget">
<input type="text" name="keyskills" placeholder="Enter Skills">
<input type="text" name="jobdescription" placeholder="Enter Job Description">
<input type="text" name="edate" placeholder="Click to enter expiry date">
<input type="text" name="cdexmin" placeholder="Enter Minimum Experience">
<input type="text" name="cdexmax" placeholder="Enter Maximum Experience">
<input type="submit">
</form>
<?php
if(isset($_POST['submit'])) {
try {
// Establish server connection and select database
$username = $_SESSION['username'];
$stmt = $db->prepare("SELECT * FROM employer INNER JOIN company ON employer.cid = company.cid WHERE employer.username='$username' ");
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$cid=$row['cid'];
$eid = $row['eid'];
$duration = $_POST['duration'];
$budget = $_POST['budget'];
$keyskills = $_POST['keyskills'];
$jobdescription = $_POST['jobdescription'];
$edate = $_POST['edate'];
$cdexmin = $_POST['cdexmin'];
$cdexmax = $_POST['cdexmax'];
$stmt = $db->prepare("INSERT INTO job(cid,eid,duration,budget,keyskills,jdesc,edate,cdexmin,cdexmax) values('$cid','$eid','$duration','$budget','$keyskills','$jobdescription','$edate','$cdexmin','$cdexmax') ");
$stmt->execute();
echo "JOB POSTED SUCCESSFULLY";
} catch(PDOException $e) {
echo "Error occurs:". $e->getMessage();
}
}
?>
Here is my code that I just created for a sample form that is trying to insert the values into database. My problem is that the values are not entering the database.
Why is it not working? Is there an syntax error I can't find?
Page parsing is easily done and it's not showing any errors but values are not entering the database.
You are using $_POST['submit'] but there is no any input with this name.
Add name to your input type submit as follow
<input type="submit" name="submit">
Change
<input type="submit">
to
<input type="submit" name="submit">
You have change in code. Add NAME attribute for POST form.
<input type="submit" name="submit">

storing data to mysql database using php

my code wont insert into the student table I dont know whats wrong could you guys possibly help? I tried to print and error message in each line to find out where the error was but I got no errors so I'm confused
<?PHP
if(isset($_POST['submit'])) {
$link = mysql_connect('localhost','root','');
if (!$link) {
die('Could not connect :' . mysql_error());
}
$Selected= mysql_select_db("elearningg", $link);
if (!$Selected) {
die("Could not connect: " . mysql_error());
}
$sql = "INSERT INTO student (FirstName, LastName, UserName,Password, confirmP ,phoneNum,Email) VALUES ('$_POST[FN]','$_POST[LN]','$_POST[userName]','$_POST[password]','$_POST[confirmPass]','$_POST[number]','$_POST[email]') ";
mysql_query($sql,$link);
mysql_close($link);
}
?>
and here's the html form:
<form method="post" action="Register.php">
<div class="contact-to">
<P> <input name="FN" type="text" class="text" value="First name" >
<input name="LN" type="text" class="text" value="Last name" style="margin-left: 10px">
<input name="userName" type="text" class="text" value="username" style="margin-left: 10px">
</div>
<div class="contact-to">
<input name="password" type="text" class="text" value="Password" style="margin-left: 10px">
<input name="confirmPass" type="text" class="text" value="Confirm password" style="margin-left: 10px">
<input name="number" type="text" class="text" value="Phone Number" >
<input name="email" type="text" class="text" value="email" >
</div>
<div> <input value="submit" type="submit" class="submit"> </div>
</form>
If you're trying to validate $_POST['submit'] after user clicks the button, you should include attribute name inside your input submit tag like this:
<input value="submit" type="submit" name="submit" class="submit">
And then you can check $_POST:
if(isset($_POST['submit'])) {
// your code
}
PHP will always return false in your condition without a attribute name...
Also, consider using mysqli with prepared statements, or PDO with prepared statements, they're much safer.
Change your sql insert query code to this:
<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
function debug($data){
echo '<pre>';
print_r($data);
echo '</pre>';
}
function getConnected($host,$user,$pass,$db) {
$mysqli = new mysqli($host, $user, $pass, $db);
if($mysqli->connect_error)
die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
return $mysqli;
}
if(isset($_POST['submit'])) {
debug('Connecting database...');
$link = getConnected('localhost','root','', 'stackoverflow');
debug('Database connected...');
$sql = "
INSERT INTO `student` (`FirstName`, `LastName`, `UserName`, `Password`, `confirmP`, `phoneNum`, `Email`)
VALUES (
'{$_POST['FN']}',
'{$_POST['LN']}',
'{$_POST['userName']}',
'{$_POST['password']}',
'{$_POST['confirmPass']}',
'{$_POST['number']}',
'{$_POST['email']}'
)";
if (!mysqli_query($link, $sql)) {
debug("Errormessage: ". mysqli_error($link)."\n");
}else{
debug('Query executed successfully...');
}
mysqli_close($link);
}
?>
Also change your form submit button code to this:
<div> <input name="submit" value="submit" type="submit" class="submit"> </div>

PHP won't post form to database

Trying to post a simple form to my database but can't get it to work. I have PHP and MySQL activated through XAMPP. The database "E-mail list" is set up with the table "Players".
PHP code:
<?php
$mysqli = new mysqli('localhost', 'root', '', 'E-mail list');
if(isset($_POST['save']))
{
$name = $mysqli->real_escape_string($_POST['name']);
$email = $mysqli->real_escape_string($_POST['email']);
$phone = $mysqli->real_escape_string($_POST['phone']);
$other = $mysqli->real_escape_string($_POST['other']);
$query = 'INSERT INTO Players (
name,
email,
phone,
other
)
VALUES ('.$name.', "'.$email.'", "'.$phone.'","'.$other.'")';
if ($mysqli->query($query))
{
echo 'Data Saved Successfully.';
}
else
{
echo 'Cannot save data.';
}}
?>
And the form:
<form id="myForm" method="post">>
<div data-role="fieldcontain">
<label for="name">Please enter your name:</label>
<input type="text" name="name" id="name" class="required" value="" autocomplete="off" />
<label for="email">Please enter your e-mail:</label>
<input type="text" name="email" id="email" value="" class="required" autocomplete="off" />
<label for="phone">Please enter your phone number:</label>
<input type="number" name="phone" id="phone" value="" class="required" autocomplete="off" />
<br><br>
<label for="other">Other comments</label>
<textarea name="other" id="other" autocomplete="off" placeholder="Anything else you'd like to add?">
</textarea>
</form>
<p><strong id="error"></strong></p>
<br><br>
<input type="button" id="save" name="save" value="Submit Form" />
<p id="response"></p>
I did some changes in your codes both PHP and HTML Parts.,
For PHP :
<?php
$mysqli = new mysqli('localhost', 'root', '', 'E-mail list');
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
if(isset($_POST['save']))
{
$name = $mysqli->real_escape_string($_POST['name']);
$email = $mysqli->real_escape_string($_POST['email']);
$phone = $mysqli->real_escape_string($_POST['phone']);
$other = $mysqli->real_escape_string($_POST['other']);
$query = "INSERT INTO Players (`name`,`email`,`phone`,`other`) VALUES ('".$name."','".$email."','".$phone."','".$other."')";
if($mysqli->query($query))
{
echo 'Data Saved Successfully.';
}
else
{
echo 'Cannot save data.';
}
}
?>
For HTML :
<form id="myForm" method="post" action="">
<div data-role="fieldcontain">
<label for="name">Please enter your name:</label>
<input type="text" name="name" id="name" class="required" value="" autocomplete="off" /><br />
<label for="email">Please enter your e-mail:</label>
<input type="text" name="email" id="email" value="" class="required" autocomplete="off" /><br />
<label for="phone">Please enter your phone number:</label>
<input type="number" name="phone" id="phone" value="" class="required" autocomplete="off" />
<br><br>
<label for="other">Other comments</label>
<textarea name="other" id="other" autocomplete="off" placeholder="Anything else you'd like to add?">
</textarea>
<p><strong id="error"></strong></p>
<br><br>
<input type="submit" id="save" name="save" value="Submit Form" />
<p id="response"></p>
</form>
I think this may help you to resolve your problem.
Missing double quotes for name value in your SQL.
VALUES ("'.$name.'", "'.$email.'", "'.$phone.'","'.$other.'")';
Use Firefox/firebug to see the parameters and result, and add an echo($query); so you can see it in firebug.
'E-mail list' doesn't seem like convenient database name, though it should be okay.
Anyway, your goal should be to display all possible error that may occur.
So, you have to always check for the errors and report them in more usable form than just 'Cannot save data.'
Always check your connect
$mysqli = new mysqli('localhost', 'root', '', 'E-mail list');
if ($mysqli->connect_error) {
trigger_error($mysqli->connect_error);
}
same for the query
if (!$mysqli->query($query)) {
trigger_error($mysqli->error." ".$query);
}
If you see no error messages - check the logic of your code: if you ever run the code, if you run the code you wrote, if PHP works, typos etc.

Categories