Jump to content

Why INSERT Fails With Prepared Statement ?


2020

Recommended Posts

Folks,

Tell me, do you see anything wrong in my INSERT ?

If not, then why is it not INSERTING ? I get no php error, nor mysql error. Button I click. Then form data vanishes as if submitted. I check db and no submission came through!

	<?php
//include('error_reporting.php');
ini_set('error_reporting','E_ALL');//Same as: error_reporting(E_ALL);
ini_set('display_errors','1');
ini_set('display_startup_errors','1');
?>
	<form name = "submit_link" method = "POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<label for="domain">Domain:</label>
<input type="text" name="domain" id="domain" placeholder="Input Domain">
<br>
<label for="domain_email">Domain Email:</label>
<input type="email" name="domain_email" id="domain_email" placeholder="Input Domain Email">
<br>
<label for="url">Url:</label>
<input type="url" name="url" id="url" placeholder="Input Url">
<br>
<label for="link_anchor_text">Link Anchor Text:</label>
<input type="text" name="link_anchor_text" id="link_anchor_text" placeholder="Input Link Anchor Text">
<br>
<textarea rows="10" cols="20">Page Description</textarea>
<br>
<label for="keywords">Keywords:</label>
<input type="text" name="keywords" id="keywords" placeholder="Input Keywords related to Page">
<br>
<button type="submit">Click me</button>
<br>
<input type="reset">
<br>
</form>
	<?php
	if($_SERVER['REQUEST_METHOD'] === 'POST')
{
    /*
    if(ISSET($_POST['submit_link']))
    {*/
        mysqli_report(MYSQLI_REPORT_ALL|MYSQLI_REPORT_STRICT);
        mysqli_connect("localhost","root","","test");
        $conn->set_charset("utf8mb4");
        
        if(mysqli_connect_error())
        {
            echo "Could not connect!" . mysqli_connect_error();
        }
        
        $query = "INSERT into links (domain,domain_email,url,link_anchor_text,page_description,keywords) VALUES (?,?,?,?,?,?)";
	        $stmt = mysqli_stmt_init($conn);
	        if(mysqli_stmt_prepare($stmt,$query))
        {
            mysqli_stmt_bind_param($stmt,'ssssss',$_POST['domain'],$_POST['domain_email'],$_POST['url'],$_POST['link_anchor_text'],$_POST['page_description'],$_POST['keywords']);
            mysqli_stmt_execute($stmt);
            
            mysqli_stmt_close($stmt);
            mysqli_close($conn);
        }
        else
        {
            die("INSERT failed!");
        }
    //}
}
	?>
	

 

@MacGuyver,

 

I will do the VALIDATIONS later. Remember, I was testing myself how much I can code without notes. What you see above was from my memory. Am still a beginner php student for 3yrs now, however! ;)

As for validation stuff will have to check notes and learn or relearn. Meaning, have to memorise the code before I add it here on current project. And so for now, ignore VALIDATIONS and let me know why it's not submitting data into db.

 

Link to comment
Share on other sites

13 minutes ago, 2020 said:

ini_set('error_reporting','E_ALL');//Same as: error_reporting(E_ALL);

the above is why you are not seeing any errors. those two are not the same. E_ALL is a defined constant. putting quotes around it makes it a string consisting of the characters E, _, A, L, and L.

  • Like 1
Link to comment
Share on other sites

Folks,

Off to sleep with a very ggrumpy mood!

Unyhappy for 2 nights now why this ain't INSERTING:

<?php
//include('error_reporting.php');
ini_set('error_reporting','E_ALL');//Same as: error_reporting(E_ALL);
ini_set('display_errors','1');
ini_set('display_startup_errors','1');
?>
	<form name = "submit_link" method = "POST" action="">
<label for="domain">Domain:</label>
<input type="text" name="domain" id="domain" placeholder="Input Domain">
<br>
<label for="domain_email">Domain Email:</label>
<input type="email" name="domain_email" id="domain_email" placeholder="Input Domain Email">
<br>
<label for="url">Url:</label>
<input type="url" name="url" id="url" placeholder="Input Url">
<br>
<label for="link_anchor_text">Link Anchor Text:</label>
<input type="text" name="link_anchor_text" id="link_anchor_text" placeholder="Input Link Anchor Text">
<br>
<textarea rows="10" cols="20">Page Description</textarea>
<br>
<label for="keywords">Keywords:</label>
<input type="text" name="keywords" id="keywords" placeholder="Input Keywords related to Page">
<br>
<input type="checkbox" name="alert_visitor_type" id="alert_visitor_type" value="Give Alert: Visitor Type">
</label for="alert_visitor_type">Give Alert: Visitor Type</lablel>
<input type="checkbox" name="alert_visitor_potential" id="alert_visitor_potential" value="Give Alert: Potential Visitor">
</label for="alert_visitor_potential">Give Alert: Potential Visitor</lablel>
<br>
<input type="radio" name="tos_agree" id="tos_agree_yes" value="yes">
<label for="tos_agree_yes">Yes:</label>
<input type="radio" name="tos_agree" id="tos_agree_no" value="no">
<label for="tos_agree_no">No:</label>
<br>
<label for="tos_agreement">Agree to TOS or not ?</label>
<select name="tos_agreement" id="tos_agreement">
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
<br>
<button type="submit">Click me</button>
<br>
<input type="reset">
<br>
</form>
	<?php
	if($_SERVER['REQUEST_METHOD'] === 'POST')
{
    /*
    if(ISSET($_POST['submit_link']))
    {*/
        mysqli_report(MYSQLI_REPORT_ALL|MYSQLI_REPORT_STRICT);
        mysqli_connect("localhost","root","","test");
        $conn->set_charset("utf8mb4");
        
        if(mysqli_connect_error())
        {
            echo "Could not connect!" . mysqli_connect_error();
        }
        
        $query = "INSERT into links (domain,domain_email,url,link_anchor_text,page_description,keywords,alert_visitor_type,alert_visitor_potential) VALUES (?,?,?,?,?,?,?,?)";
	        $stmt = mysqli_stmt_init($conn);
	        if(mysqli_stmt_prepare($stmt,$query))
        {
            mysqli_stmt_bind_param($stmt,'ssssssss',$_POST['domain'],$_POST['domain_email'],$_POST['url'],$_POST['link_anchor_text'],$_POST['page_description'],$_POST['keywords'],$_POST['alert_visitor_type'],$_POST['alert_visitor_potential']);
            mysqli_stmt_execute($stmt);
            
            mysqli_stmt_close($stmt);
            mysqli_close($conn);
        }
        else
        {
            die("INSERT failed!");
        }
    //}
}
?>

Try on your Xampp and see if you can figure-out just what the heck is wrong!

Link to comment
Share on other sites

@MacGuyver,

 

Thanks for spotting my error. I fixed it now but still problem remains. Nothing has changed. I see no mysql nor php errors and INSERT does not succeed!

 

You try:

	<?php
//include('error_reporting.php');
ini_set('error_reporting',E_ALL);//Same as: error_reporting(E_ALL);
ini_set('display_errors','1');
ini_set('display_startup_errors','1');
?>
	<form name = "submit_link" method = "POST" action="">
<label for="domain">Domain:</label>
<input type="text" name="domain" id="domain" placeholder="Input Domain">
<br>
<label for="domain_email">Domain Email:</label>
<input type="email" name="domain_email" id="domain_email" placeholder="Input Domain Email">
<br>
<label for="url">Url:</label>
<input type="url" name="url" id="url" placeholder="Input Url">
<br>
<label for="link_anchor_text">Link Anchor Text:</label>
<input type="text" name="link_anchor_text" id="link_anchor_text" placeholder="Input Link Anchor Text">
<br>
<textarea rows="10" cols="20">Page Description</textarea>
<br>
<label for="keywords">Keywords:</label>
<input type="text" name="keywords" id="keywords" placeholder="Input Keywords related to Page">
<br>
<input type="checkbox" name="alert_visitor_type" id="alert_visitor_type" value="Give Alert: Visitor Type">
</label for="alert_visitor_type">Give Alert: Visitor Type</lablel>
<input type="checkbox" name="alert_visitor_potential" id="alert_visitor_potential" value="Give Alert: Potential Visitor">
</label for="alert_visitor_potential">Give Alert: Potential Visitor</lablel>
<br>
<input type="radio" name="tos_agree" id="tos_agree_yes" value="yes">
<label for="tos_agree_yes">Yes:</label>
<input type="radio" name="tos_agree" id="tos_agree_no" value="no">
<label for="tos_agree_no">No:</label>
<br>
<label for="tos_agreement">Agree to TOS or not ?</label>
<select name="tos_agreement" id="tos_agreement">
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
<br>
<button type="submit">Click me</button>
<br>
<input type="reset">
<br>
</form>
	<?php
	if($_SERVER['REQUEST_METHOD'] === 'POST')
{
    /*
    if(ISSET($_POST['submit_link']))
    {*/
        mysqli_report(MYSQLI_REPORT_ALL|MYSQLI_REPORT_STRICT);
        mysqli_connect("localhost","root","","test");
        $conn->set_charset("utf8mb4");
        
        if(mysqli_connect_error())
        {
            echo "Could not connect!" . mysqli_connect_error();
        }
        
        $query = "INSERT into links (domain,domain_email,url,link_anchor_text,page_description,keywords,alert_visitor_type,alert_visitor_potential) VALUES (?,?,?,?,?,?,?,?)";
	        $stmt = mysqli_stmt_init($conn);
	        if(mysqli_stmt_prepare($stmt,$query))
        {
            mysqli_stmt_bind_param($stmt,'ssssssss',$_POST['domain'],$_POST['domain_email'],$_POST['url'],$_POST['link_anchor_text'],$_POST['page_description'],$_POST['keywords'],$_POST['alert_visitor_type'],$_POST['alert_visitor_potential']);
            mysqli_stmt_execute($stmt);
            
            mysqli_stmt_close($stmt);
            mysqli_close($conn);
        }
        else
        {
            die("INSERT failed!");
        }
    //}
}
	?>
	

 

Like I said, I will memorise VALIDATIOIN code then add it later. So, don;t mention about it as I havent forgotten about it.
First things first, why it not INSERTING !!!

Doing my head in for 2 nights now.

Edited by 2020
Link to comment
Share on other sites

21 hours ago, kicken said:

Perhaps you should check for errors when executing your query also.

Show me how to do it. Like this ?

	if(mysqli_stmt_execute($stmt) === FALSE)
            {
                die("Error" . mysqli_stmt_error());
            )
	

Edited by 2020
Link to comment
Share on other sites

	if(mysqli_stmt_execute($stmt) === FALSE)
            {
                die("Error\" . mysqli_stmt_error()");
            }
	

 

Working.

But I want to see how you do it.

Edited by 2020
Link to comment
Share on other sites

Issue SOLVED!

No one in this forum or another could figure out what the problem was. I got a handful of programmers in this forum and another that usually are able to help me but on this occassion everyone failed BUT one programmer on another forum who came across my thread last night or so! Some new guy to me. No offense to anyone here. Didn't come here to gloat but share my SOLUTION.

2 of my threads will be closed here and there soon thanks to him.
The issue was the buttons I was using weren't working. His button did.
Look at my current code. You will see his button (last button out of the 4) and a few (3) of my own above his. None of my buttons work. Test it for yourself!

I thought I will mention this in this forum so others can benefit.

The first 3 buttons are mine and a failure. The 4th one is his and a PASS:

	<button type="submit">Submit</button>
<button type="submit" value="submit">Submit</button>
<input type="submit" value="submit">
<button name=submit value=" ">Submit</button>
	

 

So now, I am getting response from mysql. Now got to figure-out fixing the mysql errors. I will deal with that in another thread.

Latest code ...

<?php
//include('error_reporting.php');
ini_set('error_reporting',E_ALL);//Same as: error_reporting(E_ALL);
ini_set('display_errors','1');
ini_set('display_startup_errors','1');
?>
	<form name = "submit" method = "POST" action="">
<label for="domain">Domain:</label>
<input type="text" name="domain" id="domain" placeholder="Input Domain">
<br>
<label for="domain_email">Domain Email:</label>
<input type="email" name="domain_email" id="domain_email" placeholder="Input Domain Email">
<br>
<label for="url">Url:</label>
<input type="url" name="url" id="url" placeholder="Input Url">
<br>
<label for="link_anchor_text">Link Anchor Text:</label>
<input type="text" name="link_anchor_text" id="link_anchor_text" placeholder="Input Link Anchor Text">
<br>
<textarea rows="10" cols="20">Page Description</textarea>
<br>
<label for="keywords">Keywords:</label>
<input type="text" name="keywords" id="keywords" placeholder="Input Keywords related to Page">
<br>
<input type="checkbox" name="alert_visitor_type" id="alert_visitor_type" value="Give Alert: Visitor Type">
</label for="alert_visitor_type">Give Alert: Visitor Type</lablel>
<input type="checkbox" name="alert_visitor_potential" id="alert_visitor_potential" value="Give Alert: Potential Visitor">
</label for="alert_visitor_potential">Give Alert: Potential Visitor</lablel>
<br>
<input type="radio" name="tos_agree" id="tos_agree_yes" value="yes">
<label for="tos_agree_yes">Yes:</label>
<input type="radio" name="tos_agree" id="tos_agree_no" value="no">
<label for="tos_agree_no">No:</label>
<br>
<label for="tos_agreement">Agree to TOS or not ?</label>
<select name="tos_agreement" id="tos_agreement">
<option value="yes">Yes</option>
<option value="no">No</option>
</select>
<br>
<button type="submit">Submit</button>
<button type="submit" value="submit">Submit</button>
<input type="submit" value="submit">
<button name=submit value=" ">Submit</button>
<br>
<input type="reset">
<br>
</form>
	<?php
	if($_SERVER['REQUEST_METHOD'] === 'POST')
{
    if(isset($_POST['submit']))
    {
        //Re-write the following 4 lines ...
        mysqli_report(MYSQLI_REPORT_ALL|MYSQLI_REPORT_STRICT);
        $conn = mysqli_connect("localhost","root","","test");
        $conn->set_charset("utf8mb4");
        
        $query = "INSERT into links (domain,domain_email,page_url,link_anchor_text,page_description,keywords,alert_visitor_type,alert_visitor_potential) VALUES (?,?,?,?,?,?,?,?)";
	        $stmt = mysqli_stmt_init($conn);
	        if(mysqli_stmt_prepare($stmt,$query))
        {
            mysqli_stmt_bind_param($stmt,'ssssssss',$_POST['domain'],$_POST['domain_email'],$_POST['url'],$_POST['link_anchor_text'],$_POST['page_description'],$_POST['keywords'],$_POST['alert_visitor_type'],$_POST['alert_visitor_potential']);
            if(mysqli_stmt_execute($stmt) === FALSE)
            {
                printf("Error: %s.\n", mysqli_stmt_error($stmt));
                printf("Error: %d.\n", mysqli_stmt_errno($stmt));
            }
            
            mysqli_stmt_close($stmt);
            mysqli_close($conn);
        }
        else
        {
            die("Did not INSERT!");
        }
    }
    else
    {
        die("Did Not POST->Submit!");
    }
}
else
{
    die("Did Not REQUEST_METHOD!");
}
?>

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.