Jump to content

Unable to insert data into database


~hoor~

Recommended Posts

Hello all,

I hope you are doing great.I am using phpmyadmin for the first time.So I created a table called guestbook with 4 attributes: id,name,email,message..Then I created a php file with the following code (saved in htdoc in Xampp folder)and html form which would take the data from the user and insert into the database which could later be retrieved.So far I have just written the insertion code which successfully creates the form but when I click on submit button it opens the same php file with the code and no insertion is made in the database..here is my code..plz help me urgently..i have my project deadline very soon.

php file called guest.php:

<?php

$user="root";

$password="";

$database="Guestbook";

$name=$_POST['name'];

$email=$_POST['email'];

$body=$_POST['body'];

$connect("localhost",$user,$password);

mysql($connect);

@mysql_select_db($database)or die("unable to select db");

$query = mysql_query("INSERT INTO Guestbook(id,name,email,body) VALUES('','$name','$email','$body')");

if(!$query)

{

echo mysql_errno().':'.mysql_error();

}

mysql_close();

?>

html file called new.html:

<html>

<head>

<title>Guestbook</title>

</head>

<body>

<form method="post" action="guest.php" >

Name:<input type="text" name="name" size="30"/><br/>

Email: <input type="text" name="email" size="30"/><br/>

<p>Text:<br/>

<textarea name="message" rows="15" cols="60"></textarea></p>

<br/>

<input  type="submit" value="Submit"/>

<input  type="reset" value="Clear"/>

</form>

</body>

</html>

 

Link to comment
Share on other sites

I have an error message code..

if(!$query)

{

echo mysql_errno().':'.mysql_error();

}

 

That part of your code will never be executed as the code is written.

 

you should see an error. apparently, you do not have error_reporting turned on. add this to the top of your code, before anything else.

 

error_reporting(E_ALL);
ini_set("display_errors", 1); 

Link to comment
Share on other sites

<?php

error_reporting(E_ALL);

ini_set("display_errors", 1);

mysql_close();

$user="root";

$password="";

$database="Guestbook";

$name=$_POST['name'];

$email=$_POST['email'];

$body=$_POST['body'];

$connect("localhost",$user,$password);

mysql($connect);

@mysql_select_db($database)or die("unable to select db");

$query = mysql_query("INSERT INTO Guestbook(id,name,email,body) VALUES('','$name','$email','$body')");

if(!$query)

{

echo mysql_errno().':'.mysql_error();

}

 

?>

 

Link to comment
Share on other sites

You mean like this?

<?php

 

$user="root";

$password="";

$database="Guestbook";

$name=$_POST['name'];

$email=$_POST['email'];

$body=$_POST['body'];

$connect("localhost",$user,$password);

mysql($connect);

error_reporting(E_ALL);

ini_set("display_errors", 1);

mysql_close();

@mysql_select_db($database)or die("unable to select db");

$query = mysql_query("INSERT INTO Guestbook(id,name,email,body) VALUES('','$name','$email','$body')");

if(!$query)

{

echo mysql_errno().':'.mysql_error();

}

 

?>

 

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.