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
https://forums.phpfreaks.com/topic/40421-solved-mysql-php-problem/
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..

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')";

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?

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.

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.

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');
?>

You also don't have a condition for your query...so it isn't going to know exactly what to display.

 

Try this:

 

<?php

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

while ($row = mysql_fetch_assoc($result)){

echo $row['col_name'].'<br>';

}

?>

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.