A few difficult (for me at least) PHP questions

They have: 5,633 posts

Joined: Jan 1970

Hi everyone. I've been working on a PHP and MySQL system to run my website, but have run into a brick wall with a few matters.

1. I have a seperate "products" and "articles" tables. When a user requests an article using a URL such as "articles.php?a=1" (where 'a' is the article ID). In the articles table, there is a "Product_ID" field which references that article to the relevant product, and a "Article_Type" which says whether the article is a review, preview or feature. What I need is for the page to search for products of the same ID, and a particular "Article_Type". From this result, I need to use an if-else statement to either display one of two images. The if-else statement shouldn't be too difficult, but how would I write to code to display one image if an article was found (with a hyperlink on the image to the relevant article, and another if it wasn't.

2. How can I assign a variable if it was not given in the URL. For example, if the URL is default.php?s=1 no new variable is assigned, but if only default.php is given, a script automatically assigns s=1.

I thought this would work, but for some reason it hasn't:

<?php
if(s=\"\") {
s=1;
}
?>

What am I doing wrong?

3. Finally, on my site, there is an area for volunteers to edit products, I used the following code to insert the values into the database, but it never works, failing, and showing the failed error defined in "if (!mysql_query($sql))"

<?php
$sql
= \"UPDATE TABLE products SET
Product_Name = '
$newProduct_Name',
Product_Type = '
$newProduct_Type',
Min_Specs    = '
$newMin_Specs',
Rec_Specs    = '
$newRec_Specs',
Publisher    = '
$newPublisher',
Developer    = '
$newDeveloper',
Multiplayer  = '
$newMultiplayer',
Graphics     = '
$newGraphics',
Release_Date = '
$newRelease_Date',
Price        = '
$newPrice'
WHERE Product_ID=
$id\";

if (!mysql_query(
$sql)) {
echo(\"2. A database error occurred in processing your \".
\"submission. If this error persists, please \".
\"contact <a href="
mailto:anthony@epigamer.com" class="bb-email">anthony@epigamer.com</a>.\");
}
?>

Thanks everyone, any help will be greatly appreciated as usual.

They have: 601 posts

Joined: Nov 2001

A lot more of your script would be very useful in helping you with your problems.

You know this variable you are trying to define? Does the variable get defined from a database? A much better way to write that would be:

if (!$s)
{
$s = 1;
}
'
Please note that I come from a Perl background and not a PHP one, although both language do share common properties.

Surely you have to state what kind of data structure 's' is? Is it a scalar, array, hash, monkey, elephant? Smiling

For your second question, hmm. I think I'll need more information again. How are you preparing and executing this? A few notes, however, the line:

"$sql = "UPDATE TABLE products SET"

should be replaced with:

$sql = "UPDATE products SET
'
You don't need TABLE there. In fact, that could actually be your error.

Put your last statement on a seperate line. It looks cleaner, and you can also enclose that with single quotation marks too.

...
WHERE
Product_ID = '$id'
'

Cheers

- wil

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

By default, all variables are variant. You can, however, type cast them. But you'll rarely ever see anyone do that in PHP. Examples:

<?php
// make a dummy var
$tmp= \"1234\";

// let's type cast
$myString = (string) $tmp;
$myInt = (int) $tmp;
$myBool = (bool) $tmp;

// let's settype()
$myString = settype($tmp, \"string\");
$myInt = settype($tmp, \"int\");
$myBool = settype($tmp, \"bool\");
?>

PHP Docs: type-casting, type-juggling, settype()

Mark Hensler
If there is no answer on Google, then there is no question.

They have: 601 posts

Joined: Nov 2001

Hi Mark

I wasn't claiming that all variables weren't variants, what I was saying was that he hadn't of defined what kind of data strcuture 's' is. Is it a scalar, array, hash, syscall etc.

- wil

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.