jjmusicpro Posted October 2, 2007 Share Posted October 2, 2007 Question, i have this script on a page i am running, can i make the form POST to the same page, and have it take those values and update the DB with them? how would i do that? Here is what i have. <?php if (isset($_POST['update'])){ echo "<p>Error, please try again"; } else { require_once ('connect.php'); require_once ('opendb.php'); $groupid_sent = $_GET['groupid']; $query = "SELECT * FROM zipcodes WHERE search_id='$zip_id'"; $result = @mysql_query ($query); $count = mysql_num_rows($result); //number of results while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ // Database Values $groupid_sent= $row['account_name'] ; $account_name= $row['company_name'] ; $zip_id = $row['search_id'] ; $zipcode = $row['zipcode']; $city = $row['city']; $rank = $row['rank']; $redirect_url = $row['redirect_url']; $notes_1 = $row['notes_1']; $phone_number = $row['phone_number']; $groupe_name = $row['groupe_name']; $on_off = $row['on_off']; $test_zip = $row['test_zip']; echo "Edit Zip Code <b>$zipcode</b> for $account_name"; echo "<form>"; echo "Company Name: <input type=text name=company_name1 size=75 value='$account_name' > <br>"; echo "Zipcode: <input type=text name=zipcode1 value='$zipcode' > <br>"; echo "City: <input type=text name=city1 size=50 value='$city' ><br>"; echo "Rank: <input type=text name=rank1 value='$rank' ><br>"; echo "Redirect URL: <input type=text name=redirecturl1 size=75 value='$redirect_url' > <br>"; echo "Notes: <input type=text name=notes1 value='$notes_1' ><br>"; echo "Phone Number: <input type=text name=phonenumber1 value='$phone_number' ><br>"; echo "Group Name: <input type=text name=groupname1 value='$groupe_name' ><br>"; echo "On / Off: <input type=text name=onoff1 value='$on_off' ><br>"; echo "Test Zip: <input type=text name=testzip1 value='$test_zip' ><p>"; echo "<input type=submit name=submit value=update>"; echo "</form>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/71536-question-about-post/ Share on other sites More sharing options...
MmmVomit Posted October 2, 2007 Share Posted October 2, 2007 Yes, that is actually a very common practice. Here's a file named test.php that does just that. <?php if(isset($_GET['mode']) && $_GET['mode'] == 'update') { // update database with data from $_POST } ?> <html> <head> <title> Test page </title> </head> <body> <form action="test.php?mode=update" method="post"> <input type="text" name="foo"> <input type="submit" name="submit" value="Submit"> </form> <pre> <?php // just so you can see what was posted print_r($_POST); ?> </pre> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/71536-question-about-post/#findComment-360172 Share on other sites More sharing options...
pocobueno1388 Posted October 2, 2007 Share Posted October 2, 2007 Yes you can. Something like this <?php if (isset($_POST['submit'])) { //define all your post variables here $city1 = mysql_real_escape_string($_POST['city1']); $rank1 = mysql_real_escape_string($_POST['rank1']); $query = "UPDATE table SET city1='$city1', rank1='$rank1' WHERE {condition}"; $result = mysql_query($query)or die(mysql_error()); echo "Successfully Updated"; } else { require_once('connect.php'); require_once('opendb.php'); $groupid_sent = $_GET['groupid']; $query = "SELECT * FROM zipcodes WHERE search_id='$zip_id'"; $result = @mysql_query($query); $count = mysql_num_rows($result); //number of results while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { // Database Values $groupid_sent= $row['account_name'] ; $account_name= $row['company_name'] ; $zip_id = $row['search_id'] ; $zipcode = $row['zipcode']; $city = $row['city']; $rank = $row['rank']; $redirect_url = $row['redirect_url']; $notes_1 = $row['notes_1']; $phone_number = $row['phone_number']; $groupe_name = $row['groupe_name']; $on_off = $row['on_off']; $test_zip = $row['test_zip']; echo "Edit Zip Code <b>$zipcode</b> for $account_name"; echo "<form action='{$_SERVER['PHP_SELF']}' method='post'>"; echo "Company Name: <input type=text name=company_name1 size=75 value='$account_name' > <br>"; echo "Zipcode: <input type=text name=zipcode1 value='$zipcode' > <br>"; echo "City: <input type=text name=city1 size=50 value='$city' ><br>"; echo "Rank: <input type=text name=rank1 value='$rank' ><br>"; echo "Redirect URL: <input type=text name=redirecturl1 size=75 value='$redirect_url' > <br>"; echo "Notes: <input type=text name=notes1 value='$notes_1' ><br>"; echo "Phone Number: <input type=text name=phonenumber1 value='$phone_number' ><br>"; echo "Group Name: <input type=text name=groupname1 value='$groupe_name' ><br>"; echo "On / Off: <input type=text name=onoff1 value='$on_off' ><br>"; echo "Test Zip: <input type=text name=testzip1 value='$test_zip' ><p>"; echo "<input type=submit name=submit value=update>"; echo "</form>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/71536-question-about-post/#findComment-360173 Share on other sites More sharing options...
MadTechie Posted October 2, 2007 Share Posted October 2, 2007 Basic example i think it makes sense (untested) <?php require_once ('connect.php'); require_once ('opendb.php'); if (isset($_POST['update'])) { if(isset($_POST['ID'])) { $ID = (int)$_POST['ID']; $account_name= $_POST['company_name'] ; $city = $_POST['city']; $query = "UPDATE zipcodes SET (company_name, city) VALUSE ('$account_name', '$city') WHERE search_id='$ID'"; $result = @mysql_query ($query); }else{ echo "<p>Error, please try again"; } } $groupid_sent = (int)$_GET['groupid']; $query = "SELECT * FROM zipcodes WHERE search_id='$zip_id'"; $result = @mysql_query ($query); $count = mysql_num_rows($result); //number of results while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { // Database Values $groupid_sent= $row['account_name'] ; $account_name= $row['company_name'] ; $zip_id = $row['search_id'] ; $zipcode = $row['zipcode']; $city = $row['city']; $rank = $row['rank']; $redirect_url = $row['redirect_url']; $notes_1 = $row['notes_1']; $phone_number = $row['phone_number']; $groupe_name = $row['groupe_name']; $on_off = $row['on_off']; $test_zip = $row['test_zip']; echo "Edit Zip Code <b>$zipcode</b> for $account_name"; echo "<form>"; echo "<input type='hidden' name='ID' value='".$_GET['ID']."' >"; //ADDED //You need to Fix the Types and names (include the single quotes echo "Company Name: <input type=text name=company_name1 size=75 value='$account_name' > <br>"; echo "Zipcode: <input type=text name=zipcode1 value='$zipcode' > <br>"; echo "City: <input type=text name=city1 size=50 value='$city' ><br>"; echo "Rank: <input type=text name=rank1 value='$rank' ><br>"; echo "Redirect URL: <input type=text name=redirecturl1 size=75 value='$redirect_url' > <br>"; echo "Notes: <input type=text name=notes1 value='$notes_1' ><br>"; echo "Phone Number: <input type=text name=phonenumber1 value='$phone_number' ><br>"; echo "Group Name: <input type=text name=groupname1 value='$groupe_name' ><br>"; echo "On / Off: <input type=text name=onoff1 value='$on_off' ><br>"; echo "Test Zip: <input type=text name=testzip1 value='$test_zip' ><p>"; echo "<input type=submit name=submit value=update>"; echo "</form>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/71536-question-about-post/#findComment-360174 Share on other sites More sharing options...
jjmusicpro Posted October 2, 2007 Author Share Posted October 2, 2007 thanks for the reply what do i put in the <form> element in the echo so that it will POST to itself? Quote Link to comment https://forums.phpfreaks.com/topic/71536-question-about-post/#findComment-360176 Share on other sites More sharing options...
MadTechie Posted October 2, 2007 Share Posted October 2, 2007 the example i gave you can leave as is, pocobueno1388 : won't post the form on submit, but has "mysql_real_escape_string($_POST['city1']);" which i should of added to mine (D'oh) Quote Link to comment https://forums.phpfreaks.com/topic/71536-question-about-post/#findComment-360178 Share on other sites More sharing options...
MmmVomit Posted October 2, 2007 Share Posted October 2, 2007 thanks for the reply what do i put in the <form> element in the echo so that it will POST to itself? <form action="NameOfCurrentFile" method="post"> Quote Link to comment https://forums.phpfreaks.com/topic/71536-question-about-post/#findComment-360180 Share on other sites More sharing options...
pocobueno1388 Posted October 2, 2007 Share Posted October 2, 2007 the example i gave you can leave as is, pocobueno1388 : won't post the form on submit, but has "mysql_real_escape_string($_POST['city1']);" which i should of added to mine (D'oh) Oops, I edited mine to work on submit. I do like the way MadTechie did his update query though, I never knew you could do it like that. Quote Link to comment https://forums.phpfreaks.com/topic/71536-question-about-post/#findComment-360182 Share on other sites More sharing options...
MadTechie Posted October 2, 2007 Share Posted October 2, 2007 works great with implode(",", $array); Quote Link to comment https://forums.phpfreaks.com/topic/71536-question-about-post/#findComment-360187 Share on other sites More sharing options...
jjmusicpro Posted October 2, 2007 Author Share Posted October 2, 2007 for some reason when i post back toitself, it ont display the account name, and customer name at the top anymore however, the database does update, and gives "update message" <?php if($_SERVER['REQUEST_METHOD'] == "POST"){ require_once ('connect.php'); require_once ('opendb.php'); $groupid_sent = $_GET['actname']; $query = "SELECT * FROM zipcodes WHERE account_name='$groupid_sent' group by account_name"; $result = @mysql_query ($query); $count = mysql_num_rows($result); //number of results while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ $groupname = $row['groupname'] ; $account_name= $row['company_name'] ; $zip_id = $row['search_id'] ; } }else{ require_once ('connect.php'); require_once ('opendb.php'); $groupid_sent = $_GET['groupid']; $query = "SELECT * FROM zipcodes WHERE account_name='$groupid_sent' group by account_name"; $result = @mysql_query ($query); $count = mysql_num_rows($result); //number of results while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ $groupid_sent= $row['account_name'] ; $account_name= $row['company_name'] ; $zip_id = $row['search_id'] ; }}?> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>TEST EDITOR</title> </head> <body> <?php echo "<h3>$groupid_sent - $account_name </h3>"; ?> <fieldset> <a href="index.php">Home</a> || <a href="viewzips.php?groupid=<?php echo $groupid_sent; ?>">View Active Zips</a> || <a href="pause.php?groupid=<?php echo $groupid_sent; ?>">Pause Account</a> || <a href="testzips.php?groupid=<?php echo $groupid_sent; ?>">Test Zips</a> </fieldset> <p> <?php if (isset($_POST['submit'])) { //define all your post variables here $zipid = mysql_real_escape_string($_POST['zipid1']); $city1 = mysql_real_escape_string($_POST['city1']); $rank1 = mysql_real_escape_string($_POST['rank1']); $phone_number = mysql_real_escape_string($_POST['phonenumber1']); $groupid_sent = mysql_real_escape_string($_POST['groupid1']); $account_name = mysql_real_escape_string($_POST['accountname1']); $redirect_url = mysql_real_escape_string($_POST['redirecturl1']); $notes_1 = mysql_real_escape_string($_POST['notes1']); $on_off = mysql_real_escape_string($_POST['onoff1']); $test_zip = mysql_real_escape_string($_POST['testzip1']); //Run Update Function $query = "UPDATE zipcodes SET city='$city1', rank='$rank1', redirect_url='$redirect_url', notes_1='$notes_1', phone_number='$phone_number', groupe_name='$groupid_sent', on_off='$on_off', test_zip='$test_zip' WHERE search_id='$zipid'"; $result = mysql_query($query)or die(mysql_error()); // Confirmation that updated worked echo "Zip Code Successfully Updated"; } else { require_once('connect.php'); require_once('opendb.php'); $groupid_sent = $_GET['groupid']; $query = "SELECT * FROM zipcodes WHERE search_id='$zip_id'"; $result = @mysql_query($query); $count = mysql_num_rows($result); //number of results while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { // Database Values $groupid_sent= $row['account_name'] ; $account_name= $row['company_name'] ; $zip_id = $row['search_id'] ; $zipcode = $row['zipcode']; $city = $row['city']; $rank = $row['rank']; $redirect_url = $row['redirect_url']; $notes_1 = $row['notes_1']; $phone_number = $row['phone_number']; $groupe_name = $row['groupe_name']; $on_off = $row['on_off']; $test_zip = $row['test_zip']; echo "Edit Zip Code <b>$zipcode</b> for $account_name"; echo "<form action='{$_SERVER['PHP_SELF']}' method='post'>"; echo "<input type=hidden name=zipid1 value='$zip_id>"; // Hidden Field echo "Company Name: <input type=text name=company_name1 size=75 value='$account_name' > <br>"; echo "City: <input type=text name=city1 size=50 value='$city' ><br>"; echo "Rank: <input type=text name=rank1 value='$rank' ><br>"; echo "Redirect URL: <input type=text name=redirecturl1 size=75 value='$redirect_url' > <br>"; echo "Notes: <input type=text name=notes1 value='$notes_1' ><br>"; echo "Phone Number: <input type=text name=phonenumber1 value='$phone_number' ><br>"; echo "Group Name: <input type=text name=groupname1 value='$groupe_name' ><br>"; echo "On / Off: <input type=text name=onoff1 value='$on_off' ><br>"; echo "Test Zip: <input type=text name=testzip1 value='$test_zip' ><p>"; echo "<input type=submit name=submit value=update>"; echo "</form>"; } } ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/71536-question-about-post/#findComment-360216 Share on other sites More sharing options...
MadTechie Posted October 2, 2007 Share Posted October 2, 2007 while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ $groupid_sent= $row['account_name'] ; $account_name= $row['company_name'] ; $zip_id = $row['search_id'] ; //missing values //add the other fields }}?> Quote Link to comment https://forums.phpfreaks.com/topic/71536-question-about-post/#findComment-360218 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.