Jump to content

[SOLVED] Multi submit buttons on a single form!


Stefan

Recommended Posts

I'm a newbie to PHP but there are a few things that I do know but this one caught me off guard. ???

 

As the subject points out I have been working on a HTML form with multiple submit buttons (edit insert and delete).

Each of which runs a SQL query that changes data in my database. I use a switch statement to navigate between the three . .

 

Stay with me because here is my problem!

 

When any of the three buttons are pressed everything runs smoothly apart from the fact that none of the queries do anything! If I would add new information to my table through my PHP script it would be like I never added anything at all. This would mean there would NOT even be empty lines in my table!

 

I would greatly appreciate any help . .

 

Here is parts of my script, maybe I have greatly over looked something . .

 

<form method="POST" action="newsetup.php">

  ID: <input type="text" name="id" size="10"> 

  NAME: <input type="text" name="name" size="35" value="name"> 

  ADDRESS: <input type="text" name="address" size="35" value="address"><p>

      <input type="submit" value="Insert" name="function"> 

      <!---<input type="submit" value="Edit" name="edit"> --->

      <input type="submit" value="Delete" name="function"><p>

</form>

 

<?php

 

print_r($_POST); // DEBUG - Delete "#" incase of error!

 

switch ($_POST['function'])

{

 

    case 'insert':

        $queryd = "INSERT INTO customers VALUES " . $_POST['id'] . $_POST['name'] . $_POST['address'];

        break;

 

    case 'delete':   

        $queryi = "DELETE FROM customers WHERE customers . cid = ".$_POST['id'] ."LIMIT 1";

        break;

}

 

$query = "SELECT cid, name, address FROM customers";

$handlr = mysql_query( $query, $conn ) or die('Could not get data: ' . mysql_error());

   

    // DISPLAY SELECTED INFO

    while($row = mysql_fetch_array($handlr))

        {

    // TABLE HEADERS START

            {

echo "HTML TABLE for my data".

 

?>

 

Thank you again . . to anyone willing to help.

 

regards Stefan

Link to comment
Share on other sites

Try adding single quotes in the delete command :

mysql_query("Detele from TABLE where id='$argument'");

 

Also i have noticed that there is a "space" missing

$queryi = "DELETE FROM customers WHERE customers . cid = ".$_POST['id'] ."LIMIT 1";

 

This query will result in: DELETE FROM customers WHERE customers . cid = "2"LIMIT 1

after 2" there should be a space.. :)

 

Hope this helps you :)

Link to comment
Share on other sites

My FTP client and server are NOT on friendly terms at this time, (Not your problem I know)

just wanted to let everyone know I have read their post and will replay accordingly as soon as I am able to dive back into my code.

 

To: ProjectFear

 

Thanks for pointing that out . . :-[

Q: PHP is case insensitive or does this only apply in certain conditions? Please let me know if I'm wrong.

 

To: gabarab

 

Single quotes, mmm. Sounds like a good argument.

Spaces should not give any problems, then again . . SQL can be tricky at times and that would mean I would be getting SQL errors if my queries were invalid, right?

 

To: Blade280891

 

<?php
echo "Sorry about that, didn't notice the code button.";
echo "I'll be sure to use it from now on.";
?>

 

 

Thank you again to all for taking some time to help me out.

 

Regards Stefan

 

 

Link to comment
Share on other sites

To: ProjectFear

 

Thanks for pointing that out . . :-[

Q: PHP is case insensitive or does this only apply in certain conditions? Please let me know if I'm wrong.

 

As soon as you can dive back in, fix the case to have capitals. :) It's case sensitive.

Link to comment
Share on other sites

Hi,

 

One more thing: the attribute 'name' should define a unique name for each <input> element.

 

Regards,

toivo

 

Yes..unless your creating an array, but then you need to append [],

simply put yes. or only the last value will be used..

 

or to put it in php logic

$var = "test"; //was their any point setting this?
$var = "test2";
echo $var; //test2

 

but if you mean can you use the same elements for different functions, then thats fine!

Link to comment
Share on other sites

DarkWater in this case was right! ;)

 

I wasn't getting errors or @#$! because I had NOT run the query in the first place.

To be more frank, does anybody know what this means "mysql_query" and yes thats where I went wrong.

 

I changed some statements in my switch so I'll post the code below.

 

To: ProjectFear

 

It's the first thing I checked, it does make a diffrence.

Thanks for the heads up, I'll keep my code lowercase.

 

To: gabarab

 

Your first comment gave me the idea for the new switch statements and your second argument

about the spaces was on the mark.

So thanks for that.

 

As promised, heres my changed code, hope it helps someone else!

 

$valueid = $_POST['id'];
$valuename = $_POST['name'];
$valueaddress = $_POST['address'];

switch ($_POST['function']) 
{
    case 'insert':
        $queryi = "INSERT INTO customers VALUES ('$valueid', '$valuename', '$valueaddress')";
        $handlr = mysql_query( $queryi, $conn ) or die('Could not insert data: ' . mysql_error());
        echo "data has been successfully writen to database!";
        break;

    case 'delete':    
        $queryd = "DELETE FROM customers WHERE customers . cid = ". $valueid ." LIMIT 1";
        $handlr = mysql_query( $queryd, $conn ) or die('Could not delete data: ' . mysql_error());
        echo "data has been successfully removed from database!";
        break;
}

 

I would like to thank everyone for their time and much appreciate help.

 

If anyone would like to post anymore comments on this subject be my guest.

Like I said this ones SOLVED and I'll be closing it first thing tomorrow morning.

 

Regards Stefan

Link to comment
Share on other sites

To be more frank, does anybody know what this means "mysql_query" and yes thats where I went wrong.

 

quite self explaintory mysql_query, runs a query to the mysql database e.g. mysql_query("SELECT * FROM table row");

that will select a table, and then select the row from that table

Link to comment
Share on other sites

To: gabarab

 

Spaces should not give any problems, then again . . SQL can be tricky at times and that would mean I would be getting SQL errors if my queries were invalid, right?

 

 

I have tried the code you posted....without space...and gives back error....because "limit" is stuck to the variable...therefore, the query is

select * from TABLE where ID=1LIMIT 0,5;

 

Try it....see the result...agree with me :)

Link to comment
Share on other sites

To: Blade280891

 

Yes, I know. What I meant by that was that what my code was missing in the first place.

But thanks for the eg. that should clear things up for anyone that still finds this unclear.

 

To: gabarab

 

Yes, you where right ;D if you notice the code I posted later you would notice that it reads " LIMIT 1"; and not "LIMIT 1"; as in

the first script that I put up. Again I thank you that help a lot.

 

As I mentioned before this would be my last post on the this subject and from here on out will be closed as you guys helped me

solved the problem at hand!

 

Again I say thank you and hope I can be of help to anyone in the future.

 

Signing out, Stefan

 

 

Link to comment
Share on other sites

Hmm...you don't run the query....but here is how I do things :)

 

<?php
if(isset($_POST['insert']))    {
//Enter Insert query
}

if(isset($_POST['delete']))    {
//Enter Delete query
}

if(isset($_POST['edit']))    {
//Enter Edit query
}
?>

 

This is usually what I do when I have a form with multiple submits, and it has never failed me.

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.