Jump to content

[SOLVED] MySql & PHP Problem


jadedknight

Recommended Posts

I have a small problem with MySql & PHP. First off, I can connect to the database..and supposedly submit information through a form. Yet when I am in phpmyadmin, the table shows that their are records but no data can be seen.. so anyways I tried to display the information in my index.php page and all I get is "Resource id #4". If you can help I really appreciate it! Here is my code..

 

The form/process page.

<?php

require('db-connect.php');

$title = $_POST['title'];
$author = $_POST['author'];
$date = $_POST['date'];
$content = $_POST['content'];

$query = "INSERT INTO posts VALUES ('', '$title', '$author', '$date', '$content')";
mysql_query($query);
mysql_close();

?>

<form id="form-post" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

<p>Title:</p>
<p><input class="required" id="field-title" type="text" name="title" /></p>
<p>Author:</p>
<p><input class="required" id="field-author" type="text" name="author" /></p>
<p>Date:</p>
<p><input class="required" id="field-date" type="text" name="date" /></p>
<p>Content: </p>
<p><textarea class="required" id="field-content" name="content" rows="5" cols="50"></textarea></p>
<p><input type="submit" value="Submit Post" /></p>
<p><input type="reset" value="Reset Fields" onclick="valid.reset(); return false" /></p>

</form>

 

The index.php page.

require('db-connect.php');

$query="SELECT * FROM posts";
$result=mysql_query($query);

echo $result;

Link to comment
Share on other sites

Ok I updated $query to..

$query = "INSERT INTO posts (id, title, author, date, content) VALUES ('', '$title', '$author', '$date', '$content')";

 

I then went to look into my database with phpmyadmin, and it says that there is only one record..so it is not yet submitting. Any suggestions? As for the link supplied by fert I read the manual, but I am not quite sure how to implement that code..

Link to comment
Share on other sites

Is the 'id' field set to auto-increment? If so, you don't need to include it in your query as a field name.

 

$query = "INSERT INTO posts (id, title, author, date, content) VALUES ('', '$title', '$author', '$date', '$content')";

 

Should be:

 

$query = "INSERT INTO posts (title, author, date, content) VALUES ('', '$title', '$author', '$date', '$content')";
Link to comment
Share on other sites

Assuming the columns in your table are defined in the order "id, title, author, date, content" then these are also viable alternatives

 

$query = "INSERT INTO posts (id, title, author, date, content) VALUES (NULL, '$title', '$author', '$date', '$content')";

 

$query = "INSERT INTO posts  VALUES (NULL, '$title', '$author', '$date', '$content')";

 

Do you have any other unique keys defined?

Link to comment
Share on other sites

Ok the id is auto-increment and its the primary key. This is the code I am using.

 

<?php

require('db-connect.php');

$title = $_POST['title'];
$author = $_POST['author'];
$date = $_POST['date'];
$content = $_POST['content'];

$query = "INSERT INTO posts (title, author, date, content) VALUES ('$title', '$author', '$date', '$content')";
mysql_query($query)or die(mysql_error());
mysql_close();

?>

<form id="form-post" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

<p>Title:</p>
<p><input class="required" id="field-title" type="text" name="title" /></p>
<p>Author:</p>
<p><input class="required" id="field-author" type="text" name="author" /></p>
<p>Date:</p>
<p><input class="required" id="field-date" type="text" name="date" /></p>
<p>Content: </p>
<p><textarea class="required" id="field-content" name="content" rows="5" cols="50"></textarea></p>
<p><input type="submit" value="Submit Post" /></p>
<p><input type="reset" value="Reset Fields" onclick="valid.reset(); return false" /></p>

</form>

 

I cleared the table as previously stated and I still get "Duplicate entry '0' for key 1" id is set as Not Null and I tried with Null and it still gives me the error.

Link to comment
Share on other sites

Ok! I fixed the insertion issue, I accidentally didn't set id to auto-increment after following an example :P sorry, but I still can't get the post to display..this is my display code.

 

require('db-connect.php');

 

$query="SELECT * FROM posts";

$result=mysql_query($query);

 

echo $result;

 

Thanks so much for everyones help, I am just starting to learn php and I really like it.

Link to comment
Share on other sites

Here's my Table2Table.php script

 

<?php
include 'db.php';    //connnection stuff

function table2Table($tname) {
    $result = mysql_query("SELECT * FROM `$tname` ");

    $str = "<TABLE border='1' cellpadding='4'>\n";

    // column headings
          $str .=  "<tr>\n";
          while ($fld = mysql_fetch_field ($result)) {
                   $str .= "<th>{$fld->name}</th>\n";
          }
          $str .=  "</tr>\n";
      #    echo "</tr>\n";

    // list data
          while ($row = mysql_fetch_row($result)) {
          $str .=  "<tr>\n";
          foreach ($row as $field) {
                   $str .=  "<td>$field</td>\n";
          }
          $str .=  "</tr>\n";
    }

    $str .=  "</TABLE>\n";
    
    return $str;
}

// call function
echo table2Table('posts');
?>

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.