Jump to content

Someone tell me what I'm doing wrong?


Recommended Posts

<!doctype html>
<html>
<head>
<link rel="stylesheet" href="style.css">
<meta charset="utf-8">
<title> My Practice Page </title>
</head>
<body>
<?
$first_name = '';
$last_name ='';
$city ='';
?>
$db = MYSQLI_CONNECT('localhost','root','','practiceforms');
$sql = "INSERT INTO users (first name, last name, city) VALUES
('$first_name', '$last_name', '$city')";
mysqli_query($db, $sql);
mysqli_close($db);
echo '<p> User added. </p>';
<form method="post" action="">
First Name: <input type="text" name="First Name" value="<?php $first_name;?>" <br>
Last Name: <input type="text" name="Last Name" value ="<?php $last_name;?>"<br>
City: <input type ="text" name="city" value=<?php $city; ?>" <br>
<input type="submit" name="submit" value="submit">
</body>
</html>
Link to post
Share on other sites

Perhaps you should begin with something like this...

<?phpecho "<p>Php is running</p>";error_reporting(E_ALL);ini_set('display_errors','On');$db = mysqli_connect("localhost","root","mypassword");if(!$db){  echo "<p>Unable to connect to MySQL</p>";}else if(!mysqli_select_db($db,"mydatabasename")){  echo "<p>Unable to open database</p>";}else{  echo "<p>database connected</p>"}?>
Link to post
Share on other sites

First, you should use the regular PHP tags (<?php) instead of the short tags. Short tags can be disabled. Second, your code to connect to the database is not inside a PHP block, the server thinks that is HTML instead of PHP. If you're trying to process that form, you're not getting the information that was submitted. Since the form uses the post method then the values will be in the $_POST array. You can use print_r to print the entire array to see what's in it:

print_r($_POST);
Also, this isn't doing anything:
<?php $first_name;?>
If you want to print something then use echo to print it.
Link to post
Share on other sites

It will all be in $_POST, so you can either use it straight from there or copy it into the other variables. PHP used to have a feature that would automatically create variables from things in $_POST, $_GET, $_COOKIE, etc, but that feature is pretty ancient and mostly unused because it presents security problems in poorly-written code.

Link to post
Share on other sites

So, I've revised the code to this, I get no more error messages, but my input is still not going into the table.

 

 

<!doctype html>
<html>
<head>
<link rel="stylesheet" href="style.css">
<meta charset="utf-8">
<title> My Practice Page </title>
</head>
<body>
<?php
$first_name = $_POST['first name'];
$last_name = $_POST['last name'];
$city = $_POST['city'];
$db = MYSQLI_CONNECT('localhost','root','','practiceforms');
if (!$db) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO users (first name, last name, city) VALUES ('$first_name','$last_name','$city')";
mysqli_query($db, $sql);
mysqli_close($db);
?>
<form method="post" action="">
First Name:<br>
<input type="text" name="First Name" value=""> <br>
Last Name:<br>
<input type="text" name="Last Name" value =""><br>
City:<br>
<input type ="text" name="city" value=""> <br>
<input type="submit" name="submit" value="submit">
</body>
</html>
Edited by darbok
Link to post
Share on other sites

yes I do.

 

 

I get this error.

 

Notice: Undefined index: first name in C:xampphtdocspracticeformspracticeforms.php on line 11Notice: Undefined index: last name in C:xampphtdocspracticeformspracticeforms.php on line 12Notice: Undefined index: city in C:xampphtdocspracticeformspracticeforms.php on line 13

Edited by darbok
Link to post
Share on other sites

Those errors mean that you're trying to access things in $_POST that aren't there. That will happen if you try to access $_POST without the form being submitted. You can use isset to check if something is set before trying to access it, you can use that to figure out if the form was submitted. Also, case matters. If you name your field "First Name" then you need to access it exactly like that. For the spaces in the column names in your database table, you need to surround those with backquotes.

Link to post
Share on other sites

Don't use spaces in input name attribute either, also check $_POST values hvae been set, before inserting in database table.if(isset($_POST['first_name']) && isset($_POST['last_name']) &&(isset($_POST['city'])){Code for inserting into table...}

Link to post
Share on other sites

this is the error I get now. Parse error: syntax error, unexpected ';' in C:xampphtdocspracticeformspracticeforms.php on line 24 though i think it's gotten better.

 

 

<!doctype html>

<html>
<head>
<link rel="stylesheet" href="style.css">
<meta charset="utf-8">
<title> My Practice Page </title>
</head>
<body>
<?php
$first_name = $_POST["firstname"];
$last_name = $_POST["lastname"];
$city = $_POST["city"];
$db = MYSQLI_CONNECT('localhost','root','','practiceforms');
if (!$db) {
die("Connection failed: " . mysqli_connect_error());
}
echo 'Connected successfully';
if(isset($_POST['first_name']) && isset($_POST['last_name']) &&(isset($_POST['city']));
{
$sql = "INSERT INTO users ('FIRST NAME', 'LAST NAME', 'CITY') VALUES
('$first_name','$last_name','$city')";
}
mysqli_query($db, $sql);
mysqli_close($db);
?>
<form method="post" action="">
First Name:<br>
<input type="text" name="firstname" value=""> <br>
Last Name:<br>
<input type="text" name="lastname" value =""><br>
City:<br>
<input type ="text" name="city" value=""> <br>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
Link to post
Share on other sites

thanks for the help, much appreciated, the hints and such were a good deal. To make it work I turned it into two files, one for the form and one for the php, I also changed the SQL column names to be one word all lower case instead of two words uppercase. I also took the single quote marks out of my column names.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...