Unable to insert into database

  • (2 Pages)
  • +
  • 1
  • 2

26 Replies - 10054 Views - Last Post: 14 January 2011 - 09:05 AM Rate Topic: -----

#1 Coolest_gal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 14-January 09

Unable to insert into database

Posted 06 January 2011 - 03:28 PM

Hello
Here is my php code that I want to add to a database called guestbook.I m using phpadmin..
	<?php
	$user="root";
	$password="";
	$database="Guestbook";
	mysql_connect("localhost",$user,$password);
	@mysql_select_db($database)or die("unable to select db");
	$query = mysql_query("INSERT INTO Guestbook VALUES('','abc','def','ghi')");
	mysql_close();
	?>


and this is my html file which contains a form from which I want to add data to the database
<html>
<head>
<title>blah</title>
<body>
<form action="new1.php"  method="post">
Name:<input type="text" name=" Name" size="30"/><br/>
Email: <input type="text" name="email" size="30"/><br/>
<p>Text:<br/>
<textarea name="body" rows="15" cols="60"></textarea></p>
<br/>
<input tabindex="9" type="submit" value="Submit"/>
<input tabindex="10" type="reset" value="Clear"/>
</form>
</body>
</html>


So what I did as I created a table successfully in phpmyadmin thats all..now Im trying to insert data but not working..I would liek to make a bottom adding guestbook..

This post has been edited by Dormilich: 06 January 2011 - 03:31 PM
Reason for edit:: please use [code] /* your code source here */ [/code] tags when posting code


Is This A Good Question/Topic? 0
  • +

Replies To: Unable to insert into database

#2 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3485
  • View blog
  • Posts: 10,043
  • Joined: 08-June 10

Re: Unable to insert into database

Posted 06 January 2011 - 03:33 PM

you never check whether your query succeeds.
Was This Post Helpful? 0
  • +
  • -

#3 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: Unable to insert into database

Posted 06 January 2011 - 04:13 PM

What Dormilich just said. Your not doing anything if the query fails.
$query = mysql_query("INSERT INTO Guestbook VALUES('','abc','def','ghi')") or die(mysql_error());




Do that and see what happens :)
Was This Post Helpful? 0
  • +
  • -

#4 stevo86  Icon User is offline

  • New D.I.C Head

Reputation: 13
  • View blog
  • Posts: 33
  • Joined: 05-January 11

Re: Unable to insert into database

Posted 06 January 2011 - 06:08 PM

Few little things for you to correct to make sure it's all in order.

1. Close your <head> tag in your HTML file.

<html>
<head>
<title>blah</title>
</head> <!-- here -->
<body>
<form action="new1.php"  method="post">
<!-- Name:<input type="text" name=" Name" size="30"/><br/>  extra space before name, watch out for this -->
Name:<input type="text" name="name" size="30" /><br /> <!-- this is how I would do it -->
Email: <input type="text" name="email" size="30"/><br/>
<p>Text:<br/></p> <!-- Probably want to close your paragraph tags as well. </p> -->
<textarea name="body" rows="15" cols="60"></textarea></p>
<br/>
<input tabindex="9" type="submit" value="Submit"/>
<input tabindex="10" type="reset" value="Clear"/>
</form>
</body>
</html>



2. Now for your insertion query.

<?php
// Database details.
$user="root";
$password="";
$database="Guestbook";
// Grab post data.
$name = $_POST['name'];
$email = $_POST['email'];
$msg = $_POST['body'];
// Connect
mysql_connect("localhost",$user,$password);
@mysql_select_db($database)or die("unable to select db");
// Query. Notice the parenthesis after Guestbook()
//$query = mysql_query("INSERT INTO Guestbook VALUES('','abc','def','ghi')");
$query = mysql_query("INSERT INTO Guestbook(id, name, email, body) VALUES('', '$name', '$email', '$msg')");
// Error checking
if (!$query)
{
  echo mysql_errno().':'.mysql_error(); // This gives you the mysql error number and error string so you can Google for help
}
// Close connection
mysql_close();
?>



Hopefully that explains things.
Was This Post Helpful? 0
  • +
  • -

#5 Coolest_gal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 14-January 09

Re: Unable to insert into database

Posted 07 January 2011 - 03:42 PM

Thanks people!
The problem still exists..When I click on submit button I get another page which has the SQL code on it...Probably there is something wrong with creating the database? So I downloaded xampp..I go to phpmyadmin and create a database called as Guestbook which has a Guestbook table..I save the new1.php file in htdocs inside the xampp directory and the other one at some other location........Are these steps correct? Do I need to use expoert/import..if yes then how?

sorry not the SQL but I get another page which has the same php code on it..
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3485
  • View blog
  • Posts: 10,043
  • Joined: 08-June 10

Re: Unable to insert into database

Posted 07 January 2011 - 04:02 PM

for simple test cases you can create an SQLite DB on the fly.

just an example:
$pdo = new PDO("sqlite::memory:");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

# create 'animals' table
$pdo->exec("
    CREATE TABLE `animals`
    (
        `id`   INTEGER PRIMARY KEY,
        `name` TEXT NOT NULL,
        `type` TEXT NOT NULL
    )"
);
# insert data query
$setup = $pdo->prepare("
    INSERT INTO
        `animals` (`name`, `type`)
    VALUES
        (:name, :type)"
);
$setup->bindParam("name", $name);
$setup->bindParam("type", $species);

# table data
$animals = array(
    "pussy"  => "cat",
    "tweety" => "bird",
    "waldi"  => "dog",
    "tom"    => "cat",
    "jerry"  => "mouse",
    "idefix" => "dog"
);

# write to DB
foreach ($animals as $name => $species)
{
    $setup->execute();
}

Was This Post Helpful? 0
  • +
  • -

#7 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 926
  • View blog
  • Posts: 3,204
  • Joined: 19-January 10

Re: Unable to insert into database

Posted 07 January 2011 - 04:50 PM

Take out the @ symbol on line 6. There is no reason why it should be there.


(@ should be banned)
Was This Post Helpful? 1
  • +
  • -

#8 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: Unable to insert into database

Posted 07 January 2011 - 05:09 PM

Hi,

To clarify,what exactly are you seeing? Are you seeing your form? If so what happens when you submit it? Did you append the mysql_error() call when you try to insert? What do you mean by getting another page with the same php code on it? What are you seeing?
Was This Post Helpful? 0
  • +
  • -

#9 Coolest_gal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 14-January 09

Re: Unable to insert into database

Posted 08 January 2011 - 12:06 AM

@Jstall...Yes I can see the form and when I click the submit button it takes me another page which has the same php code and this page opens in Opera..I am using mac btw.

I wrote the exact code as posted by stevo.
Was This Post Helpful? 0
  • +
  • -

#10 Coolest_gal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 14-January 09

Re: Unable to insert into database

Posted 09 January 2011 - 11:59 PM

guys plz help me.......:(
Was This Post Helpful? 0
  • +
  • -

#11 atik97  Icon User is offline

  • ???
  • member icon

Reputation: 144
  • View blog
  • Posts: 715
  • Joined: 16-September 08

Re: Unable to insert into database

Posted 10 January 2011 - 01:05 AM

As you have said you are viewing the php code, which means your server isn't running.

Are you accessing the html form directly? I know its a silly question to ask, but sometimes such mistakes may happen.

First make sure you put both your html form and php script under the htdocs folder. If your html file is form.html and you have put it in the htdocs folder, access it from browser as http://localhost/form.html and when you will submit the form, it will call the processing script (which is new1.php in your case). In processing script, try to echo out the form variables to make sure you are getting expected data.

print_r($_POST);



Put the line at top of your new1.php and notice what happen after you submit html form. If you are getting right values, then move to the next part, which is connecting to database and try to insert the data.
Was This Post Helpful? 1
  • +
  • -

#12 Coolest_gal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 14-January 09

Re: Unable to insert into database

Posted 10 January 2011 - 02:53 AM

woohooo!!! its done finally..I can't believe my mistake was this silly and I spent like two days trying to figure it out..I was accessing my html form directly...thanks a bunch ..atik!!!God bless you!!
Was This Post Helpful? 0
  • +
  • -

#13 atik97  Icon User is offline

  • ???
  • member icon

Reputation: 144
  • View blog
  • Posts: 715
  • Joined: 16-September 08

Re: Unable to insert into database

Posted 10 January 2011 - 03:01 AM

Good to know it works finally. Now take a look at what other people recommends you to make your script better, which will definitely help you in the long run.

This post has been edited by atik97: 10 January 2011 - 03:04 AM

Was This Post Helpful? 0
  • +
  • -

#14 Coolest_gal  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 37
  • Joined: 14-January 09

Re: Unable to insert into database

Posted 10 January 2011 - 07:38 AM

ok now what I have done is ..I have placed both the html and php code in Guestbook.php files under htdoc and its working perfectly fine..Now I want another php code for retrieving data from the database can I place it in the same Guestbook.php file and where?
Was This Post Helpful? 0
  • +
  • -

#15 atik97  Icon User is offline

  • ???
  • member icon

Reputation: 144
  • View blog
  • Posts: 715
  • Joined: 16-September 08

Re: Unable to insert into database

Posted 10 January 2011 - 08:59 AM

How you will design your application depends on you. First decide how will your application work. May be you want to show database records when you first load the page. Then there might be a form to add new records or a link which will open a form for adding new records. This is just a sample of many alternatives. Finalize your decision and then implement it. For example, to implement above mentioned pattern, at the top of the page put the code for establishing database connection and make database query to retrieve result sets and display it. Then you may put the rest of the code. Hope it will give you some idea.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2