Showing posts with label PHP $_GET. Show all posts
Showing posts with label PHP $_GET. Show all posts

Friday, 2 August 2019

How to convert all $_POST or $_GET values to Integers

By using the built-in PHP functions  array_map and intval.
  • array_map = Applies the callback to the elements of the given arrays, this applies the function for each value of an array (PHP 4 >= 4.0.6, PHP 5)
  • intval = Get the integer value of a variable (PHP 4, PHP 5)
Examples:
{codecitation style=”brush: PHP;”}
$post_int = array_map(‘intval’, $_POST); //Converting every post value to integer
$get_int = array_map(‘intval’, $_GET); //Converting every get value to integer
{/codecitation}

Wednesday, 5 September 2018

The PHP / MYSQL Update query does not work

Can anyone tell my why this update query is not working?

if ($_GET['update']) {
include 'config.php';
//Connect to MYSQL Database server
$connect = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die("Could not connect to MYSQL Database.");
$result = mysql_select_db(DB_NAME, $connect) or die("Could not connect to MYSQL table.");

mysql_query("UPDATE contact SET read = 1 WHERE id = '$_GET[update]'")or die("Query failed.");
echo "Update works!";
} else {
echo "Update does not work...ughh.";
}


I got the query to work. For anyone who was worrying about the security, I was using this script as a test to see if I wanted to use it. I just added the security now that the script works. Thank you all for the help and tips.

What is column read?
mysql_query("UPDATE contact SET read = 1 WHERE id = '$_GET[update]'")

Judging by the non-capitalization of read, I suspect you are using a reserved word in MySQL for that column.
See:
To Get around this, just put a single quote around read. I.E.
mysql_query("UPDATE contact SET 'read' = 1 WHERE id = '$_GET[update]'")

Or better per j.bruni:
mysql_query("UPDATE contact SET `read` = 1 WHERE id = '$_GET[update]'")

Friday, 31 August 2018

echo $ _GET ['id'] does not display anything

I have a page in which a user chooses a recipe which has a unique ID, they are then sent to another page where the information for that recipe is displayed but it is not displaying anything.

$recipes = mysql_query("
        SELECT DISTINCT `name`, `step1`, `step2`, `step3`, `image`, `reference`
        FROM `recipe` r
        INNER JOIN `recipe_ingredients` ri
        ON r.id = ri.recipe_id
        WHERE ri.ingredient_id IN (".$ingredient1.",".$ingredient2.",".$ingredient3.")
    ");

This is the query on the main page, this allows the user to choose a set of ingredients and then retrieve recipes from them.
echo '<form action="recipe.php?id=<?php echo $recipe_id;?>" method="POST">';
        echo '<input id="search" name="Look" type="Submit" value="Look" >';
        echo '</form>';

Under each recipe there is a button a user presses, this will send the user to the specific recipe.
$sql = mysql_query("SELECT `name`, `image`
FROM `recipe`
WHERE `id` = ".$_GET['id']." LIMIT 1");

This is the query I'm running on the results page (recipe.php)
Further down the page I ran this.
<?php
echo $_GET['id'];
?>

It didn't display an ID and in the url I was displayed with this:
"recipe.php?id="
There is data in the table so what am I doing wrong?

The following echo syntax is incorrect:
echo '<form action="recipe.php?id=<?php echo $recipe_id;?>" method="POST">';

should be:
echo '<form action="recipe.php?id='.$recipe_id.'" method="POST">';

I don't see where you are setting $recipe_id, which appears to be the root of your problem.
It is unclear where you are trying to retrieve the elusive $recipe_id from.
If your trying to pull it in from $recipes then you should include it in your sql statement:
SELECT `id`,....

then pull it in after your $recipes->fetch_assoc():
$recipe_id = $recipes['id'];