10 Replies - 825 Views - Last Post: 14 May 2016 - 09:30 AM Rate Topic: -----

#1 yodamatte   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 17-March 16

Wont go it to the if-statment

Posted 13 May 2016 - 11:05 AM

The submits name is addCart and using the method post, were the if the submit is "clicked" it used echo Cart.
But as for now nothing i happening and I have no clue what´s wrong.

<?php
if(!empty($_GET['product'])){
    $stmt = $db->prepare("SELECT * FROM produkter WHERE produkt_id = {$_GET['product']}");	
    $stmt->execute();
  if($produkter = $stmt->fetch()){
				
echo<<<singleProdukt
<form  Action="SlutProjket.php?product={$produkter['produkt_id']}" method="post">				   
  <input type="submit" alt="Submit" name="addCart" value="Lägg till i kassan">
  <input type="hidden" name="produktid" value="{$produkter['produkt_id']}">
</form>

singleProdukt;


if(isset($_POST['addCart'])){
		echo "Cart";
	$stmt = $db->prepare("INSERT INTO produktOrder(produkt_id) VALUES('{$_POST['produktid']}')");
	$stmt->execute();
		}
?>



This post has been edited by yodamatte: 13 May 2016 - 11:06 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Wont go it to the if-statment

#2 astonecipher   User is offline

  • Enterprise Software Architect
  • member icon

Reputation: 3150
  • View blog
  • Posts: 11,955
  • Joined: 03-December 12

Re: Wont go it to the if-statment

Posted 13 May 2016 - 11:26 AM

Use prepared statements for one thing.

You want the processing done at the top of the script for one thing. You may have an issue with an enclosed bracket,
if($produkter = $stmt->fetch()){




Is error reporting on? Have you checked the error logs?
Was This Post Helpful? 0
  • +
  • -

#3 yodamatte   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 17-March 16

Re: Wont go it to the if-statment

Posted 13 May 2016 - 11:28 AM

Well the
if($produkter = $stmt->fetch()){

works fine, there´s no error, the if-statment just dosnt echo the written message "Cart";

This post has been edited by Dormilich: 17 May 2016 - 12:41 AM

Was This Post Helpful? 0
  • +
  • -

#4 astonecipher   User is offline

  • Enterprise Software Architect
  • member icon

Reputation: 3150
  • View blog
  • Posts: 11,955
  • Joined: 03-December 12

Re: Wont go it to the if-statment

Posted 13 May 2016 - 11:36 AM

Do a test, try this:


if ( $_SERVER['REQUEST_METHOD'] == 'POST') {


AND once again, you are defeating the purpose of the prepared statement, when you are dropping the variable into the sql statement.
Was This Post Helpful? 0
  • +
  • -

#5 yodamatte   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 17-March 16

Re: Wont go it to the if-statment

Posted 13 May 2016 - 11:39 AM

SO just change this line if(isset($_POST['addCart'])){ to that on u just posted?

In that case I get the same result, no errors, no echo, nothing

This post has been edited by astonecipher: 13 May 2016 - 11:50 AM
Reason for edit:: Removed quote from previous response

Was This Post Helpful? 0
  • +
  • -

#6 astonecipher   User is offline

  • Enterprise Software Architect
  • member icon

Reputation: 3150
  • View blog
  • Posts: 11,955
  • Joined: 03-December 12

Re: Wont go it to the if-statment

Posted 13 May 2016 - 11:51 AM

As a test, yes. Your processing should still be above, but that should check if the form is posting.
Was This Post Helpful? 0
  • +
  • -

#7 yodamatte   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 17-March 16

Re: Wont go it to the if-statment

Posted 13 May 2016 - 11:55 AM

And How should I see if itīs posting or not, cause Iīm literlly getting nothing.
Was This Post Helpful? 0
  • +
  • -

#8 astonecipher   User is offline

  • Enterprise Software Architect
  • member icon

Reputation: 3150
  • View blog
  • Posts: 11,955
  • Joined: 03-December 12

Re: Wont go it to the if-statment

Posted 13 May 2016 - 11:56 AM

If this doesn't work, you need to post the entire page. You only giving a snippet is guessing the way the page is structured.


Add this to the top of the script, right after the opening php tag,
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
	echo "Cart";
}

Was This Post Helpful? 0
  • +
  • -

#9 yodamatte   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 17-March 16

Re: Wont go it to the if-statment

Posted 13 May 2016 - 12:01 PM

As it ainīt working you want me to send you the whole code?
I have only been writting in a file, so looks kind of strange.
Was This Post Helpful? 0
  • +
  • -

#10 astonecipher   User is offline

  • Enterprise Software Architect
  • member icon

Reputation: 3150
  • View blog
  • Posts: 11,955
  • Joined: 03-December 12

Re: Wont go it to the if-statment

Posted 13 May 2016 - 12:18 PM

Post the file in code tags so we can see everything going on. Hopefully you have made some updates.
Was This Post Helpful? 0
  • +
  • -

#11 CTphpnwb   User is online

  • D.I.C Lover
  • member icon

Reputation: 3837
  • View blog
  • Posts: 13,994
  • Joined: 08-August 08

Re: Wont go it to the if-statment

Posted 14 May 2016 - 09:30 AM

Using a variable in the query: $db->prepare("SELECT * FROM produkter WHERE produkt_id = {$_GET['product']}"); defeats the purpose of preparing it! You're promising the parser that the query is safe, but it's not because it contains information provided by the user. You can never trust the user!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1