Gondie Posted April 29, 2011 Share Posted April 29, 2011 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. Quote Link to comment Share on other sites More sharing options...
fugix Posted April 30, 2011 Share Posted April 30, 2011 what is the exact line that is giving this error? Quote Link to comment Share on other sites More sharing options...
Gondie Posted April 30, 2011 Author Share Posted April 30, 2011 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. Quote Link to comment Share on other sites More sharing options...
fugix Posted April 30, 2011 Share Posted April 30, 2011 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? Quote Link to comment Share on other sites More sharing options...
Gondie Posted April 30, 2011 Author Share Posted April 30, 2011 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 Quote Link to comment Share on other sites More sharing options...
fugix Posted April 30, 2011 Share Posted April 30, 2011 the $_POST array will not have anything to do with values from mysql Quote Link to comment Share on other sites More sharing options...
Gondie Posted April 30, 2011 Author Share Posted April 30, 2011 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'. Quote Link to comment Share on other sites More sharing options...
fugix Posted April 30, 2011 Share Posted April 30, 2011 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 Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted April 30, 2011 Share Posted April 30, 2011 Your hidden input's name attribute is malformed. Its currently <input name'num' . . . Needs an equal sign in it. Quote Link to comment Share on other sites More sharing options...
Gondie Posted April 30, 2011 Author Share Posted April 30, 2011 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. Quote Link to comment Share on other sites More sharing options...
fugix Posted April 30, 2011 Share Posted April 30, 2011 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... Quote Link to comment Share on other sites More sharing options...
Gondie Posted April 30, 2011 Author Share Posted April 30, 2011 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. Quote Link to comment Share on other sites More sharing options...
fugix Posted April 30, 2011 Share Posted April 30, 2011 in your first script..the name of your <select> is "page"...so wouldnt it be $_POST['page'] not $_POST['num']? Quote Link to comment Share on other sites More sharing options...
Gondie Posted April 30, 2011 Author Share Posted April 30, 2011 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.