RCS Posted April 8, 2008 Share Posted April 8, 2008 OK I'm trying to create a script so that I can delete information form a table row I want to have the option to choose what row I want to delete and I'm not sure how to do this without creating this $id=$_GET['id']; { // open database connection $connection = mysql_connect($db_host, $db_user, $db_passwd) or die ("Unable to connect!"); // select database mysql_select_db($db_name) or die ("Unable to select database!"); // generate and execute query $query = "DELETE FROM upload WHERE id = '1'"; $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error()); // close database connection mysql_close($connection); // print result echo "<font size=-1>Deletion successful. <br><br><a href=update.php>Go back to the main page</a></font>"; } ?> <form action="delete.php" method="POST" enctype="multipart/form-data" name="delete"> <blockquote> <p align="center"> </p> <p> <select name="select" id="3"> <option>Update</option> <option>1</option> <option selected>2</option> <option>3</option> <option>4</option> <option>5</option> <option>6</option> <option>7</option> <option>8</option> <option>9</option> <option>10</option> </select> </p> <p align="center"> <input type="submit" name="Submit" id="Submit" value="Submit"> </p> </form> How do I get script to read the number that I choose from the dropdown menu as row I want deleted?? on a new page for every row that I want to delete, I don't want to do that, I guess what I'm saying is how do I make it so that on the front end when I choose what row I want to delete say from a dropdown menu, how do I get the script I made above to read what row I picked on the form page. Hope this makes sense. Thanks in advance. Quote Link to comment Share on other sites More sharing options...
cgm225 Posted April 8, 2008 Share Posted April 8, 2008 please first read http://catb.org/~esr/faqs/smart-questions.html Quote Link to comment Share on other sites More sharing options...
RCS Posted April 8, 2008 Author Share Posted April 8, 2008 please first read http://catb.org/~esr/faqs/smart-questions.html OK what are you implying??? Quote Link to comment Share on other sites More sharing options...
RCS Posted April 8, 2008 Author Share Posted April 8, 2008 ???????????????? Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted April 8, 2008 Share Posted April 8, 2008 I think cgm was mostly referring to the hopelessly vague title of your topic. PHP HELP!! surely not? I wasn't aware this was a PHP forum... Anyways, your question was a bit unclear. Is the code you included something you tried? In the form you showed, im not entirely sure why you've specified an enctype - you dont need it. You've called your drop down 'select' and are using the POST method. But in the PHP, you appear to be trying to select this from a variable, id, in the GET array. Or perhaps i missed the point. You might need to try and explain more carefully. Quote Link to comment Share on other sites More sharing options...
craygo Posted April 8, 2008 Share Posted April 8, 2008 well first you want your script to check and see if you have clicked the submit button. Then run the query if you have. <?php if(isset($_POST['submit'])){ // run query $id=$_POST['select']; // open database connection $connection = mysql_connect($db_host, $db_user, $db_passwd) or die ("Unable to connect!"); // select database mysql_select_db($db_name) or die ("Unable to select database!"); // generate and execute query $query = "DELETE FROM upload WHERE id = '$id'"; $result = @mysql_query($query); if($result){ echo "Delete Successfull<br />"; } else { die ("Error in query: $query. " . mysql_error()."<br />"); } // close database connection mysql_close($connection); // print result echo "<font size=-1><a href=update.php>Go back to the main page</a></font>"; } else { //show form ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data" name="delete"> <p align="center"> </p> <p> <select name="select" id="3"> <option>Update</option> <option>1</option> <option selected>2</option> <option>3</option> <option>4</option> <option>5</option> <option>6</option> <option>7</option> <option>8</option> <option>9</option> <option>10</option> </select> </p> <p align="center"> <input type="submit" name="Submit" id="Submit" value="Submit"> </p> </form> <?php } ?> Name the page anything you like and you can accomplish it all on one page. Ray Quote Link to comment Share on other sites More sharing options...
RCS Posted April 8, 2008 Author Share Posted April 8, 2008 I'm just trying to create a script that I can delete information from a mysql table row. I want to be able to choose what row I want to delete from a form with a drop down menu. <form action="delete.php" method="POST" name="delete"> <p align="center"> </p> <select name="select" id="select"> <option>1</option> <option selected>2</option> <option>3</option> <option>4</option> <option>5</option> <option>6</option> <option>7</option> <option>8</option> <option>9</option> <option>10</option> </select> </p> <p align="center"> <input type="submit" name="Submit" id="Submit" value="Submit"> </p> </form> I know that I can create a script like $connection = mysql_connect($db_host, $db_user, $db_passwd) or die ("Unable to connect!"); // select database mysql_select_db($db_name) or die ("Unable to select database!"); // generate and execute query $query = "DELETE FROM upload WHERE id = '1'"; $result = mysql_query($query) or die ("Error in query: $query. " . mysql_error()); // close database connection mysql_close($connection); // print result echo "<font size=-1>Deletion successful. <a href=update.php>Go back to the main page[/url]</font>"; To delete one row but how do I get it to delete the row I choose from form?? Quote Link to comment Share on other sites More sharing options...
RCS Posted April 8, 2008 Author Share Posted April 8, 2008 would I not need some kind of array like form field 1 = id 1 or something?? Quote Link to comment Share on other sites More sharing options...
craygo Posted April 8, 2008 Share Posted April 8, 2008 Just copy and paste this code. <?php /***************************************/ if(isset($_POST['Submit'])){ // run query $id=$_POST['select']; // You named your dropdown select not id // open database connection $connection = mysql_connect($db_host, $db_user, $db_passwd) or die ("Unable to connect!"); // select database mysql_select_db($db_name) or die ("Unable to select database!"); // generate and execute query $query = "DELETE FROM upload WHERE id = '$id'"; $result = @mysql_query($query); if($result){ echo "Delete Successfull<br />"; } else { die ("Error in query: $query. " . mysql_error()."<br />"); } // close database connection mysql_close($connection); // print result echo "<a href=update.php>Go back to the main page[/url]</font>"; } else { //show form ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" enctype="multipart/form-data" name="delete"> <p align="center"> </p> <p> <select name="select" id="3"> <option>Update</option> <option>1</option> <option selected>2</option> <option>3</option> <option>4</option> <option>5</option> <option>6</option> <option>7</option> <option>8</option> <option>9</option> <option>10</option> </select> </p> <p align="center"> <input type="submit" name="Submit" id="Submit" value="Submit"> </p> </form> <?php } ?> add your connection variables for the database. Put it any file you like and just run it. Ray Quote Link to comment Share on other sites More sharing options...
lewis987 Posted April 8, 2008 Share Posted April 8, 2008 This code is all ok (by craygo) apart from the HTML part. Ive corrected it below for you: (I assume that the code is going to be included into another page, or this is just a fraction of the code) <?php // open database connection $connection = mysql_connect($db_host, $db_user, $db_passwd) or die ("Unable to connect!"); // select database mysql_select_db($db_name) or die ("Unable to select database!"); if(isset($_POST['submit'])){ // Get ID $id=mysql_real_esacpe_string($_POST['select']); // generate and execute query $query = "DELETE FROM upload WHERE id = '{$id}'"; $result = @mysql_query($query); if($result){ echo "Delete Successfull<br />"; } else { die ("Error in query: $query. " . mysql_error()."<br />"); } // close database connection mysql_close($connection); // print result echo "<font size=-1><a href=update.php>Go back to the main page</a></font>"; } else { //show form //AND EXECUTE QUERY TO GET ALL THE ID's IN THE DATABASE: $SQL = "SELECT * FROM `upload` ORDER BY `id` ASC"; $Q = mysql_query($SQL) echo('<form action="'.$_SERVER['PHP_SELF'].'" method="POST" enctype="multipart/form-data" name="delete">'); echo('<p align="center"> </p> <p>'); if(mysql_num_rows($Q) > 0){ echo('<select name="select" id="3"> <option>Update</option>'); while($DATA = mysql_fetch_assoc($Q){ echo('<option value="'.$DATA['id'].'">'.$DATA['id'].'</option>'); } echo('</select>'); }else{ echo('There are no records in the database!'); } echo(' </p> <p align="center"> <input type="submit" name="Submit" id="Submit" value="Submit"> </p> </form>'); } ?> Quote Link to comment Share on other sites More sharing options...
craygo Posted April 8, 2008 Share Posted April 8, 2008 I just took his form. Not going to create everything for him. Quote Link to comment Share on other sites More sharing options...
RCS Posted April 8, 2008 Author Share Posted April 8, 2008 Thanks for the help. I am getting a error Parse error: syntax error, unexpected $end in Quote Link to comment Share on other sites More sharing options...
craygo Posted April 8, 2008 Share Posted April 8, 2008 he forgot a parenthesis. <?php // open database connection $connection = mysql_connect($db_host, $db_user, $db_passwd) or die ("Unable to connect!"); // select database mysql_select_db($db_name) or die ("Unable to select database!"); if(isset($_POST['submit'])){ // Get ID $id=mysql_real_esacpe_string($_POST['select']); // generate and execute query $query = "DELETE FROM upload WHERE id = '{$id}'"; $result = @mysql_query($query); if($result){ echo "Delete Successfull<br />"; } else { die ("Error in query: $query. " . mysql_error()."<br />"); } // close database connection mysql_close($connection); // print result echo "<font size=-1><a href=update.php>Go back to the main page</a></font>"; } else { //show form //AND EXECUTE QUERY TO GET ALL THE ID's IN THE DATABASE: $SQL = "SELECT * FROM `upload` ORDER BY `id` ASC"; $Q = mysql_query($SQL); echo('<form action="'.$_SERVER['PHP_SELF'].'" method="POST" enctype="multipart/form-data" name="delete">'); echo('<p align="center"> </p>'); echo ('<p>'); if(mysql_num_rows($Q) > 0){ echo('<select name="select" id="3"> <option>Update</option>'); while($DATA = mysql_fetch_assoc($Q)){ echo('<option value="'.$DATA['id'].'">'.$DATA['id'].'</option>'); } echo('</select>'); } else { echo('There are no records in the database!'); } echo(' </p> <p align="center"> <input type="submit" name="Submit" id="Submit" value="Submit"> </p> </form>'); } ?> Ray Quote Link to comment Share on other sites More sharing options...
RCS Posted April 8, 2008 Author Share Posted April 8, 2008 now I get error Fatal error: Call to undefined function: mysql_real_esacpe_string() Quote Link to comment Share on other sites More sharing options...
RCS Posted April 8, 2008 Author Share Posted April 8, 2008 this is the html form i'm using in addition to that script form is in another file called update.php <form name="delete" method="POST" action="delete.php"> <p>Select the sales ad you would like to delete <select name="select" id="select"> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> <option value="4">4</option> <option value="5">5</option> <option value="6">6</option> <option value="7">7</option> <option value="8">8</option> <option value="9">9</option> <option value="10">10</option> </select> </p> <p> <input type="submit" name="submit" id="submit" value="Submit"> </p> </form> this is the script that does the work delete.php <?php include("dbinfo.inc"); // open database connection $connection = mysql_connect($db_host, $db_user, $db_passwd) or die ("Unable to connect!"); // select database mysql_select_db($db_name) or die ("Unable to select database!"); if(isset($_POST['submit'])){ // Get ID $id=mysql_real_esacpe_string($_POST['select']); // generate and execute query $query = "DELETE FROM upload WHERE id = '{$id}'"; $result = @mysql_query($query); if($result){ echo "Delete Successfull<br />"; } else { die ("Error in query: $query. " . mysql_error()."<br />"); } // close database connection mysql_close($connection); // print result echo "<font size=-1><a href=update.php>Go back to the main page</a></font>"; } else { //show form //AND EXECUTE QUERY TO GET ALL THE ID's IN THE DATABASE: $SQL = "SELECT * FROM `upload` ORDER BY `id` ASC"; $Q = mysql_query($SQL); echo('<form action="'.$_SERVER['PHP_SELF'].'" method="POST" ">'); echo('<p align="center"> </p>'); echo ('<p>'); if(mysql_num_rows($Q) > 0){ echo('<select name="select" id="select"> <option>Update</option>'); while($DATA = mysql_fetch_assoc($Q)){ echo('<option value="'.$DATA['id'].'">'.$DATA['id'].'</option>'); } echo('</select>'); } else { echo('There are no records in the database!'); } echo(' </p> <p align="center"> <input type="submit" name="Submit" id="Submit" value="Submit"> </p> </form>'); } ?> Quote Link to comment Share on other sites More sharing options...
craygo Posted April 8, 2008 Share Posted April 8, 2008 i spell it wrong mysql_real_escape_string() Just fix it Quote Link to comment Share on other sites More sharing options...
RCS Posted April 8, 2008 Author Share Posted April 8, 2008 Can I somehow make the script also so that I can update information in a row??? Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted April 8, 2008 Share Posted April 8, 2008 Whoops. Ignore. 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.