Jump to content

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.

 

 

 

 

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.