Jump to content

Trying to insert simple info into database


Shockhazard30

Recommended Posts

I am new to mysql and php but I am trying to build a page that will hold all the links I frequently use. I would like to put a place on it to insert a link but what I have come up with is not working.

 

any one have any ideas?

 

 

<HTML>

<HEAD>

<TITLE> </TITLE>

</HEAD>

<BODY>

 

 

<?php

$dbConn = mysql_connect("localhost", "root", "accessdb79");

  if (!$dbConn){

  print "<h3>problem connecting to database...</h3>\n";

  } // end if

 

?>

<form method = "post">

<input type = "text" name = "address" value = "">

<input type = "text" name = "linkdisplay" value = "">

<input type="hidden" name= "task" value= "insert">

<input type = "submit" value = "Add Link"></form>

<?php

 

if ($task == "insert"){

$query = "INSERT INTO `main`.`links` (`pkey`, `address`, `linkdisplay`) VALUES('null', '$address', '$linkdisplay');";

mysql_query($query, $dbConn);

} // end if

 

?>

</BODY>

</HTML>

should it look more like this?

 

if ($task == "insert"){
$query = "INSERT INTO `main`.`links` (`pkey`, `address`, `linkdisplay`) VALUES('null', '$_POST[address]', '$_POST[linkdisplay]')";
mysql_query($query, $dbConn);
} // end if

 

I tried this and changed no other code but it still won't insert into the db

There are 2 issues I see.

 

1. You need to select a database with mysql_select_db.

2. You should not be passing 'null'  which is string = "null" for the first parameter.  I'm assuming that's an auto_increment integer key.  If so, either pass null without the single quotes, or better yet, omit it entirely from the column list and values.

 

I can't say for sure, but probably:

 

if ($task == "insert"){
  $query = "INSERT INTO `main`.`links` (`address`, `linkdisplay`) VALUES ('$_POST[address]', '$_POST[linkdisplay]')";
  $result = mysql_query($query, $dbConn);
  if (!$result) {
    // Log out mysql_error();
  }
} // end if

 

The main issue however, is that to catch errors you need to grab the result and if the result is false, check the mysql_error() function to determine what the problem is.  Then you don't have to guess.

 

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.