Jump to content

newbie question


witchy478

Recommended Posts

Hi I'm trying to insert some information into my database but it tells me that the name is undefined how to I define it so that it works.

 

Here is my code

<html>
<body>

<form action="insert.php" method="post">
name: <input type="text" name="name" />
email: <input type="text" name="email" />
password: <input type="text" name="password" />

<input type="submit" />
</form>

</body>
</html>
<?php
$link = mysql_connect('localhost','test',''); 
if (!$link) { 
die('Could not connect to MySQL: ' . mysql_error()); 
} 
mysql_close($link); 

$sql="INSERT INTO emails (name,email, password)
VALUES
('$_POST[name]','$_POST[email]','$_POST[password]')";

mysql_close($con)
?>

Link to comment
Share on other sites

A question about your code, are you not showing all of it?

 

The reason I ask is because I'm not seeing you ever select a database, you are closing your connection immediately after you close it with mysql_close, and lastly at the end of the code you are closing another connection presumably.  The one at the end is unnecessary, since the connection will be closed upon the script fully executing. 

Link to comment
Share on other sites

Try this....

 

<html>
<body>

<form action="insert.php" method="post">
name: <input type="text" name="name" />
email: <input type="text" name="email" />
password: <input type="text" name="password" />

<input type="submit" />
</form>

</body>
</html>
<?php
$link = mysqli_connect('localhost','test',''); 
if (!$link) { 
die('Could not connect to MySQL: ' . mysql_error()); 
} 
$con = mysqli_connect($link); 

$sql="INSERT INTO emails (name,email, password)
VALUES
('$_POST[name]','$_POST[email]','$_POST[password]')";

mysqli_query=($con, $sql);

mysqli_close($con);
?>

Link to comment
Share on other sites

I've done that and now I get these errors

 

Warning: mysqli_connect() expects parameter 1 to be string, object given in C:\wamp\www\test\insert.php on line 19

 

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\wamp\www\test\insert.php on line 25

 

Warning: mysqli_close() expects parameter 1 to be mysqli, null given in C:\wamp\www\test\insert.php on line 27

Link to comment
Share on other sites

You need to wrap your code in a big if statement to make it so that it will only run once the form has been submitted.

 

Right now when you load the page it will attempt to insert your data, but there is nothing to insert since you have not yet submitted the data with the form.

 

You also only need to call mysqli_connect once, your doing it twice right now (the second time incorrectly).  You need to use the correct variable for the connection when using the other mysqli calls.

 

<html>
<body>

<form action="insert.php" method="post">
name: <input type="text" name="name" />
email: <input type="text" name="email" />
password: <input type="text" name="password" />

<input type="submit" />
</form>

</body>
</html>
<?php

//Check that post data is received
if (count($_POST) > 0){
$link = mysqli_connect('localhost','test',''); 
if (!$link) { 
	die('Could not connect to MySQL: ' . mysqli_error($link)); 
} 

$sql="INSERT INTO emails (name,email, password)
VALUES
('{$_POST['name']}','{$_POST['email']}','{$_POST['password']}')";

mysqli_query($link, $sql);

mysqli_close($link);
}

?>

 

Link to comment
Share on other sites

I finally solved the problem I did the coding like this

 

<?php

$host="localhost"; // Host name 
$username=""; // Mysql username 
$password=""; // Mysql password 
$db_name="test"; // Database name 
$tbl_name="emails"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// Get values from form 
$name=$_POST['name'];
$email=$_POST['email'];
$password=$_POST['password'];

// Insert data into mysql 
$sql="INSERT INTO $tbl_name(name, email, password)VALUES('$name', '$email', 'password')";
$result=mysql_query($sql);

// if successfully insert data into database, displays message "Successful". 
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='insert.php'>Back to main page</a>";
}

else {
echo "ERROR";
}

// close connection 
mysql_close();
?>

 

and it worked.

 

Thanks for your help

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.