soniccool
soniccool

Reputation: 6058

On If Statement Insert into DB

I want to make an if statement that inserts something specific into MySQL database when user clicks submit. But for some reason its not working i think my if statement below is wrong but im not sure how to phrase it. if($_POST and $_POST['action'] == 'submit'){ What do i put here properly to make it so that if the user presses submit it inserts this into the db?

if($_POST and $_POST['action'] == 'submit'){ 
    foreach($_POST as $k=>$v){
        $$k = $v;
    }
    foreach($cat as $k=>$v){
        if($v =='') continue;
        dbConnect("INSERT INTO twit_info(cat_id) values('". $v ."')");
    }
}

Below is the submit portion of my code.

<form id="add_tweet_form" action="<?=$u?>admin/submit.php" enctype="multipart/form-data" method="post">
<input type="hidden" value="<?=$user_info->id_str?>" name="twitid">
</form>
<form id="go_back_to_user" action="submit" method="post">
<input type="hidden" value="masud" name="username">
<input type="hidden" value="from_tweet" name="submit_username">
</form>
<nav>
<a class="btn float-right" onclick="document.forms['add_tweet_form'].submit();this.innerHTML='Submitting...';" href="javascript:;">Submit</a>
<a class="float-right nav-text gray" title="Don't submit tweet, and go back"  onclick="window.location.href='<?=$u?>admin/submit.php';" href="javascript:;">Cancel</a>

Upvotes: 1

Views: 110

Answers (2)

Mathlight
Mathlight

Reputation: 6653

mayby make this line:

if($_POST and $_POST['action'] == 'submit'){ 

to something like this:

if(isset($_POST['action']) && $_POST['action'] == 'submit'){ 

The forms:

<form id="add_tweet_form" action="<?=$u?>admin/submit.php" enctype="multipart/form-data" method="post">
    <input type="hidden" value="<?=$user_info->id_str?>" name="twitid"/>
    <input type="hidden" value="submitted" name="PostAction" />
</form>
<form id="go_back_to_user" action="submit" method="post">
    <input type="hidden" value="masud" name="username">
    <input type="hidden" value="from_tweet" name="submit_username">
</form>
<nav>
    <a class="btn float-right" onclick="document.forms['add_tweet_form'].submit();this.innerHTML='Submitting...';" href="javascript:;">Submit</a>
    <a class="float-right nav-text gray" title="Don't submit tweet, and go back"  onclick="window.location.href='<?=$u?>admin/submit.php';" href="javascript:;">Cancel</a>

The prosessing:

if($_POST['PostAction']) && $_POST['PostAction'] == 'submitted'){ 
    echo '<pre>';
    print_r($_POST); // to see if the right info is posted...
    echo '</pre>';
    foreach($_POST as $k=>$v){
        $$k = $v;
    }
    foreach($cat as $k=>$v){
        if($v =='') continue;
        dbConnect("INSERT INTO twit_info(cat_id) values('". $v ."')");
        echo 'We have inserted something <br />';
    }
}

Upvotes: 1

Michael
Michael

Reputation: 10474

add <input type="hidden" name="add_tweet_form" value="true">

then check if(isset($_POST['add_tweet_form']) && $_POST['add_tweet_form']=='true')

Upvotes: 0

Related Questions