Jump to content

PHP HELP!!


RCS

Recommended Posts

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.

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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??

 

 

 

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>');

}
?>

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>');

 

}

?>

 

 

 

 

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.