Jump to content

Archived

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

nashyboy

Need Help - Probably Simple!!!

Recommended Posts

Hi,

Quite new to this.... Can't understand why when i add to a table and echo the sql command, it does not display the id. I have set the id field as auto_increment and primary key and adds to table correctly but when i retrieve all the data from table it doesnt show the id's....

Is there some sort of php configuration i need to change or perhaps i've set up the table uincorrect.

CREATE TABLE messages (
  id tinyint(5) NOT NULL auto_increment,
  messagestatus varchar(20) NOT NULL default '',
  time varchar(20) NOT NULL default '',
  date varchar(20) NOT NULL default '',
  requestor varchar(80) NOT NULL default '',
  requestorstatus varchar(20) NOT NULL default '',
  message blob NOT NULL,
  PRIMARY KEY  (id)
) TYPE=MyISAM;

Can someone give me some pointers?

Thanks

Paul

Share this post


Link to post
Share on other sites
Im using phpmyadmin and yes it does appear that the id exists and when i echo the query out after adding it from the form although it adds it to the form in phpmyadmin it echos all except the id which gets shown like this...

INSERT INTO users (id, firstname, surname, email, password) VALUES('', 'John', 'Smith' , 'john.smith@bestbitter.com' , 'smooth' )

The form code is

<form action="added_user.php" method="post">
              <table width="600" border="0" cellspacing="2" cellpadding="2">
                <tr>
                  <td width="138"><strong></strong></td>
                  <td width="462"><strong><font color="#000000" size="1" face="Verdana, Arial, Helvetica, sans-serif">
                    <input name="id" type="hidden">
                    </font></strong></td>
                </tr>
                <tr>
                  <td><strong><font color="#000000" size="1" face="Verdana, Arial, Helvetica, sans-serif">Firstname</font></strong></td>
                  <td><strong><font color="#000000" size="1" face="Verdana, Arial, Helvetica, sans-serif">
                    <input name="firstname" type="text" size="30" maxlength="80">
                    </font></strong></td>
                </tr>
                <tr>
                  <td><strong><font color="#000000" size="1" face="Verdana, Arial, Helvetica, sans-serif">Surname</font></strong></td>
                  <td><strong><font color="#000000" size="1" face="Verdana, Arial, Helvetica, sans-serif">
                    <input name="surname" type="text" size="30" maxlength="80">
                    </font></strong></td>
                </tr>
                <tr>
                  <td><strong><font color="#000000" size="1" face="Verdana, Arial, Helvetica, sans-serif">Email</font></strong></td>
                  <td><strong><font color="#000000" size="1" face="Verdana, Arial, Helvetica, sans-serif">
                    <input name="email" type="text" size="30" maxlength="80">
                    </font></strong></td>
                </tr>
                <tr>
                  <td><strong><font color="#000000" size="1" face="Verdana, Arial, Helvetica, sans-serif">Password</font></strong></td>
                  <td><strong><font color="#000000" size="1" face="Verdana, Arial, Helvetica, sans-serif">
                    <input name="password" type="text" size="30" maxlength="20">
                    </font></strong></td>
                </tr>
                <tr>
                  <td><font color="#000000">&nbsp;</font></td>
                  <td><font color="#000000">&nbsp;</font></td>
                </tr>
                <tr>
                  <td><font color="#000000">&nbsp;</font></td>
                  <td><strong><font color="#000000" size="1" face="Verdana, Arial, Helvetica, sans-serif">
                    <input type="reset" name="Reset" value="Reset">
                    <input name="Submit" type="submit" id="Submit" value="Submit">
                    </font></strong></td>
                </tr>
              </table>
</form>

and

the added page is...

            <?php

$id=$_POST['id'];
$firstname=$_POST['firstname'];
$surname=$_POST['surname'];
$email=$_POST['email'];
$password=$_POST['password'];

?>

<?php

include("..\includes\login_details.inc");

mysql_connect ($dbhost, $dbuser, $dbpass) or die (mysql_error()); //Connects to database

echo $dbhost. "<br/>";
echo $dbuser. "<br/>";
echo $dbpass. "<br/>";
echo $dbname. "<br/>";


mysql_select_db ($dbname) or die (mysql_error()); //Selects your database

$query = "INSERT INTO users (id, firstname, surname, email, password) VALUES('$id', '$firstname', '$surname' , '$email' , '$password' )";

echo $query. "<br/>";

$result = mysql_query($query) or die("Error: ". mysql_error());

?>

Share this post


Link to post
Share on other sites
your inserting information in the database where the query to see the information fom the database.

Share this post


Link to post
Share on other sites
remove the following from the form:

<input name="id" type="hidden">

remove the following from the added page:
$id=$_POST['id'];

change the following:
$query = "INSERT INTO users (id, firstname, surname, email, password) VALUES('$id', '$firstname', '$surname' , '$email' , '$password' )";
to:
$query = "INSERT INTO users (firstname, surname, email, password) VALUES('$firstname', '$surname' , '$email' , '$password' )";

echo $query. "<br/>";

$result = mysql_query($query) or die("Error: ". mysql_error());

to view the id:
$id=mysql_insert_id($result);
echo $id;

Share this post


Link to post
Share on other sites
Okay thanks. I have done all that and added a new entry. When i come to edit that entry it is not parsing the id value from the results page to the edit_message.php page.

I know the hyperlink code in the results page is correct as it shows edit_message.php?id=5 in the status bar.

The current page i have to parse the value and then edit is.....

<?php

$id=$_POST['id'];

include("includes/login_details.inc");

mysql_connect ($dbhost, $dbuser, $dbpass) or die (mysql_error()); //Connects to database

mysql_select_db ($dbname) or die (mysql_error()); //Selects your database

$sql = "SELECT * FROM messages where id = '$id' ";



    $result = mysql_query($sql);

echo $sql;

    if (!$result) {
        echo "Could not successfully run query ($sql) from DB: " . mysql_error();
        exit;
    }
   
    if (mysql_num_rows($result) == 0) {
        echo "No Messages Currently Live";

    }

    // While a row of data exists, put that row in $row as an associative array
    // Note: If you're expecting just one row, no need to use a loop
    // Note: If you put extract($row); inside the following loop, you'll
    //      then create $userid, $fullname, and $userstatus etc
    while ($row = mysql_fetch_assoc($result)) {
echo "<table width=800 border=0 cellspacing=0 cellpadding=0>
          <tr>
              <td width=25>&nbsp;</td>
              <td><font color=#00CC33 size=3 face=Georgia, Times New Roman, Times, serif><strong>Update
              Message<br>
            <form action=edited_message.php method=post>
              <table width=600 border=0 cellspacing=2 cellpadding=2>
                <tr>
                  <td width=138><strong></strong></td>
                  <td width=462><input name=id type=text value=";
        echo $row["id"];
echo "><input name=messagestatus type=text id=messagestatus value=";
        echo $row["messagestatus"];
echo "<br><strong>";
        echo $row["requestor"];
echo " | ";
        echo $row["date"];
echo " | ";
        echo $row["time"];
echo " | <font color=#0066FF><a href='edit_message.php?id=";
echo $row["id"];
echo "'>Update Message</strong></font></td>";
echo "</tr></table><br>";
    }
       
    mysql_free_result($result);

mysql_close();


?>

Share this post


Link to post
Share on other sites
I still having problems as per last post - can anyone help please. Cant figure it out....

Share this post


Link to post
Share on other sites
nope none at all. Just not parsing the id value to the next page and displaying it even though it shows in the hyperlink of the previous.

Share this post


Link to post
Share on other sites
Hi,
change this:
$id=$_POST['id'];

to:
$id=$_GET['id'];

I havent gone through your entire code but that was the first thing I noticed
POST works when submitting values from forms while GET works using urls.

Share this post


Link to post
Share on other sites

×

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.