Jump to content

POST Data, Forms, and Database


Gondie

Recommended Posts

Iv tried asking a lot of people and have had no luck resolving this issue, so I will try here.

 

I have created a Form which gathers its information from a Database. The first form is a Dropdown Option, which when submitted takes you to another page. The new page is Supposed to use the value from the Dropdown to search the database and return the proper rows to populate a few text areas to edit the values. This is where I am stuck.

 

Compared to what most of you create this is probably sloppy and not too well structured, but I am new to this. Anyways, I will provide more information below now.

 

The following is the initial page with the first form. This is where you would select what page you wish to edit. (I believe this may be where the issue resides) - (I included the 'num' value before the 'name' value so that I could see it is getting the value from the database, which it is)

 

<form method="post" action="pageedit.php"><br />    
<?php
include "config.php"; 
    echo "<select name=\"page\">\n";

        $conn = mysql_connect("localhost", "$username", "$password");
        
        if (!$conn) {
            echo "Unable to connect to DB: " . mysql_error();
            exit;
        }
          
        if (!mysql_select_db("$database")) {
            echo "Unable to select $database: " . mysql_error();
            exit;
        }
        
        $sql = "SELECT * FROM sitePages";
        
        $result = mysql_query($sql);
        
        if (!$result) {
            echo "Could not successfully run query ($sql) from DB: " . mysql_error();
            exit;
        }
        
        if (mysql_num_rows($result) == 0) {
            echo "No rows found, nothing to print so am exiting";
            exit;

        }
        
        while ($row = mysql_fetch_assoc($result)) {
            echo "<option value='";
            echo $row['num'];
            echo "'>";
            echo $row['num'];
            echo " - ";
            echo $row['name'];
            echo "</option>";

        }
        
    echo "</select>";
?>
<br />
<input type="submit" />
</form> 

 

And then the following is the page that the form is sent to when submitted: (I have attempted to Echo the 'num' value sent so that I could verify that it is indeed sent, but it is not. I will post the message I receive after the PHP snippet

 

<form method="post" action="pageeditinsert.php"><br />
<?php
include "config.php";
echo $_POST['num']; 
$num=$_POST['num'];

	$conn = mysql_connect("localhost", "$username", "$password");

	if (!$conn) {
		echo "Unable to connect to DB: " . mysql_error();
		exit;
	}
	  
	if (!mysql_select_db("$database")) {
		echo "Unable to select $database: " . mysql_error();
		exit;
	}

	$sql = "SELECT * FROM sitepages WHERE num = '$num'";

	$result = mysql_query($sql);

	if (!$result) {
		echo "Could not successfully run query ($sql) from DB: " . mysql_error();
		exit;
	}

	if (mysql_num_rows($result) == 0) {
		echo "No rows found, nothing to print so am exiting";
		exit;

	}

	while ($row = mysql_fetch_assoc($result)) {
		echo "<input name'num' type='hidden' class='form1' value='";
		echo $num;
		echo "' maxlength='10' id='cat' /><br /><br /><br />";
		echo "<input name='name' type='text' class='form1' value='";
		echo $row['name'];
		echo "' maxlength='20' id='name' /><br /><br />";
		echo "<input name='desc' type='text' class='form1' value='";
		echo $row['desc'];
		echo "' maxlength='100' id='desc' /><br /><br /><br />";
		echo "<input name='title' type='text' class='form1' value='";
		echo $row['title'];
		echo "' maxlength='100' id='title' /><br /><br /><br />";
		echo "<input name'cat' type='hidden' class='form1' value='cat' maxlength='3' id='cat' /><br /><br /><br />";


	}
?>
  <br /><br /><br />
  <center><input type="submit" /><input type="reset" /></center>
</form>

 

Below is the message I receive when attempting to echo the POSTed 'num' value:

 

Notice: Undefined index: num in C:\wamp\www\gondieCOM\editor\edit\pageedit.php on line 16

Call Stack

# Time Memory Function Location

1 0.0025 687600 {main}( ) ..\pageedit.php:0

 

(I am running the latest WAMP release on my personal PC for testing purposes until I have finished this and upload it to my hosting server)

 

Anyways thanks for your time. Hopefully someone has an idea for me.

Link to comment
Share on other sites

echo $_POST['num']; 
$num=$_POST['num'];

 

These 2 lines. I only copied the error from the first, but they both produce it.

 

When it executes the query it returns the function for no rows found

 

if (mysql_num_rows($result) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;

 

If I alter the code so that it does not use a variable to search the database, instead using (for example) 1, it performs the query flawlessly.

Link to comment
Share on other sites

im confused as to where you are getting $_POST['num']; from...in order for that to work..you would need to have an input in you first form on with the name of "num".... what value are you trying to pass to pageedit.php?

Link to comment
Share on other sites

            echo "<option value='";
            echo $row['num'];
            echo "'>";
            echo $row['num'];
            echo " - ";
            echo $row['name'];
            echo "</option>";

 

Shouldnt the value be accepted rather than the name in this case?

 

The above code creates this dropdown

 

editor2.jpg

 

editor3.jpg

Link to comment
Share on other sites

As I stated in my origional post:

 

I included the 'num' value before the 'name' value so that I could see it is getting the value from the database, which it is

 

It is the same number that is being entered as the options 'value'.

Link to comment
Share on other sites

yes I editted my answer as I saw this after I posted...but to get that the $num value...it isnt actually passed from the form...you would set up another query like the one that you did in the first script..and use the $row['num'] value to make your second query

Link to comment
Share on other sites

Thank you for pointing out the missing '=' I had completely missed it, but that is not the issue at hand.

 

I am sorry fugix but I seem to be misunderstanding something. The form is set up to POST the value selected TO the next page, which contains the next form. That is why I had to declare the $num variable form the $POST_ data.

 

Below is the same method which I have implemented on another page, which instead of being used to Edit an entry it is to create one. So do not get this confused with the php I have posted earlier. This is simply to state that it does function as I believe.

 

   <form method="post" action="pageinsert.php"><br />
  <input name="name" type="text" class="form1" value="About Us" maxlength="20" id="name" /><br /><br />
  <input name="desc" type="text" class="form1" value="Learn more about us" maxlength="100" id="desc" /><br /><br /><br />
  <input name="title" type="text" class="form1" value="About Us" maxlength="100" id="desc" /><br /><br /><br />
<?php
include "config.php"; 
echo "<select name=\"cat\">\n";

	$conn = mysql_connect("localhost", "$username", "$password");

	if (!$conn) {
		echo "Unable to connect to DB: " . mysql_error();
		exit;
	}
	  
	if (!mysql_select_db("$database")) {
		echo "Unable to select $database: " . mysql_error();
		exit;
	}

	$sql = "SELECT * FROM siteNavCats";

	$result = mysql_query($sql);

	if (!$result) {
		echo "Could not successfully run query ($sql) from DB: " . mysql_error();
		exit;
	}

	if (mysql_num_rows($result) == 0) {
		echo "No rows found, nothing to print so am exiting";
		exit;

	}

	while ($row = mysql_fetch_assoc($result)) {
		echo "<option value='";
		echo $row['num'];
		echo "'>";
		echo $row['name'];
		echo "</option>";

	}

echo "</select>";
?>
  <br /><br /><br />
  <input type="hidden" name="num" value="1" />
  <center><input type="submit" /><input type="reset" /></center>
</form

 

Which is sent to the following, and works flawlessly:

 

<?
include "config.php";

$num=$_NUM['num'];
$name=$_POST['name'];
$desc=$_POST['desc'];
$title=$_POST['title'];
$cat=$_POST['cat'];


$con = mysql_connect("localhost","$username","$password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("$database", $con);

$sql="INSERT INTO sitePages VALUES ('$num','$name','$desc','$title','cat')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "$name    $desc    $title    $cat";
echo "Added Page";

mysql_close($con);

  $url = htmlspecialchars($_SERVER['HTTP_REFERER']);
  echo "<br><br><a href='$url'>back</a>";
?> 

 

Now, if I am still missing something I am sorry.

Link to comment
Share on other sites

this

<input name="name" type="text" class="form1" value="About Us" maxlength="20" id="name" /><br /><br />

 

corresponds to this

$name=$_POST['name'];

 

because you have an input with the same name...on the first scripts that you showed me...the only input that you have on that page is the submit button...so that is the only value that will fill the $_POST array...

 

Link to comment
Share on other sites

But it still sends the $cat variable from an option menu of the same format

 

echo "<select name=\"cat\">\n";

	$conn = mysql_connect("localhost", "$username", "$password");

	if (!$conn) {
		echo "Unable to connect to DB: " . mysql_error();
		exit;
	}
	  
	if (!mysql_select_db("$database")) {
		echo "Unable to select $database: " . mysql_error();
		exit;
	}

	$sql = "SELECT * FROM siteNavCats";

	$result = mysql_query($sql);

	if (!$result) {
		echo "Could not successfully run query ($sql) from DB: " . mysql_error();
		exit;
	}

	if (mysql_num_rows($result) == 0) {
		echo "No rows found, nothing to print so am exiting";
		exit;

	}

	while ($row = mysql_fetch_assoc($result)) {
		echo "<option value='";
		echo $row['num'];
		echo "'>";
		echo $row['name'];
		echo "</option>";

	}

echo "</select>";
?>

 

Options have No name field, only an ID field. I have tried entering the ID as the 'num' value but it did not work.

Link to comment
Share on other sites

I will try that. And as of my previous posts sorry, I was mistaken. I had not completed the dropdown and it is not functioning properly.

 

Thank you, this is resolved! The error was in assigning the $num variable with the wrong $POST_ entry.

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.