For Programmers: Free Programming Magazines  


Home > Archive > PHP DB > January 2007 > Re: [PHP-DB] Am I missing something?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: [PHP-DB] Am I missing something?
Stephen Johnson

2007-01-27, 9:58 pm

My first thought is that your values in your insert statement are not in
quotes..

>
> $insert = 'INSERT INTO domains ( picture, thumbnail ) values(
> $picture, $thumbnail )';


Should be

$insert = 'INSERT INTO domains ( picture, thumbnail ) values( "$picture",
"$thumbnail")';

Hope that helps.

<?php
/*

Stephen Johnson c | eh
The Lone Coder

http://www.ouradoptionblog.com
Join our journey of adoption

http://www.thelonecoder.com
stephen@thelonecoder.com

continuing the struggle against bad code

*/
?>


> From: Alexander <kurokawa@tds.net>
> Date: Sat, 27 Jan 2007 19:55:17 -0600
> To: PHP-DB <php-db@lists.php.net>
> Subject: [PHP-DB] Am I missing something?
>
> I fixed my code and it shows up fine now. It says it supposedly enters the
> data when I input the fields but when I check the MySQL database, the info
> isn't there.
>
> Here's the fixed code if you wish to test it:
>
> <html>
>
> <body>
>
> <?php
>
> function add_to_table( $picture, $thumbnail ) {
> $picture = mysql_real_escape_string($picture);
> $thumbnail = mysql_real_escape_string($thumbnail);
>
> $user = "user";
> $pass = "password";
> $database = "mydata";
>
> $link = mysql_connect("localhost", $user, $pass);
> if(!$link) {
> echo 'Error in linking';
> }
>
> $db = mysql_select_db($database, $link);
>
> $insert = 'INSERT INTO domains ( picture, thumbnail ) values(
> $picture, $thumbnail )';
> $indata = mysql_query( $insert, $link );
>
>
> }
>
> function write_form() {
> echo "<form method='post' action='{$_SERVER['PHP_SELF']}'>";
>
> echo "<p>Enter url of large picture: <input type='text'
> name='picture' />";
>
> echo "<p>Enter url of thumbnail: <input type='text'
> name='thumbnail' />";
>
> echo "<p><input type='submit' value='Add to Database' />";
>
> echo "</form>";
> }
>
> if ( !empty( $_REQUEST['picture'] ) && !empty(
> $_REQUEST['thumbnail'] ) ) {
> $input = add_to_table( $_REQUEST['picture'],
> $_REQUEST['thumbnail'] );
> if(!input) {
> echo 'Error\!';
> }
> else {
> echo 'Data enter successfully\!';
> $idreturn = mysql_insert_id($input);
> echo 'Image inserted to ID ' . $idreturn;
> }
> }
> else {
> write_form();
> }
>
> ?>
>
> </body>
>
> </html>
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>

Alexander

2007-01-27, 9:58 pm

No change.

I echod my insert statement and got : INSERT INTO domains ( picture,
thumbnail ) values( "$picture", "$thumbnail" )

excatly like that. Shouldn't the values echo as what should be inserted into
the database?

----- Original Message -----
From: "Stephen Johnson" <maillists@thelonecoder.com>
To: "Alexander" <kurokawa@tds.net>; "PHP-DB" <php-db@lists.php.net>
Sent: Saturday, January 27, 2007 8:01 PM
Subject: Re: [PHP-DB] Am I missing something?


> My first thought is that your values in your insert statement are not in
> quotes..
>
>
> Should be
>
> $insert = 'INSERT INTO domains ( picture, thumbnail ) values( "$picture",
> "$thumbnail")';
>
> Hope that helps.
>
> <?php
> /*
>
> Stephen Johnson c | eh
> The Lone Coder
>
> http://www.ouradoptionblog.com
> Join our journey of adoption
>
> http://www.thelonecoder.com
> stephen@thelonecoder.com
>
> continuing the struggle against bad code
>
> */
> ?>
>
>
>
>

Alexander

2007-01-27, 9:58 pm

Alright, this may be the problem:

Fatal error: Call to undefined function mysql_real_escape_string() in
C:\apache2triad\htdocs\gallery.php on line 10

which is refering to :

$picture = mysql_real_escape_string($picture);


/* never again use direct code from an example in a book, there's always an
error */

----- Original Message -----
From: "Alexander" <kurokawa@tds.net>
To: "PHP-DB" <php-db@lists.php.net>
Sent: Saturday, January 27, 2007 8:13 PM
Subject: Re: [PHP-DB] Am I missing something?


> No change.
>
> I echod my insert statement and got : INSERT INTO domains ( picture,
> thumbnail ) values( "$picture", "$thumbnail" )
>
> excatly like that. Shouldn't the values echo as what should be inserted
> into the database?
>
> ----- Original Message -----
> From: "Stephen Johnson" <maillists@thelonecoder.com>
> To: "Alexander" <kurokawa@tds.net>; "PHP-DB" <php-db@lists.php.net>
> Sent: Saturday, January 27, 2007 8:01 PM
> Subject: Re: [PHP-DB] Am I missing something?
>
>
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>

Stephen Johnson

2007-01-27, 9:58 pm

What version of php are using? It is odd that mysql_real_escape_string is
undefined..

Alternately you could addslashes...
Hope this helps

<?php
/*

Stephen Johnson c | eh
The Lone Coder

http://www.ouradoptionblog.com
Join our journey of adoption

http://www.thelonecoder.com
stephen@thelonecoder.com

continuing the struggle against bad code

*/
?>


> From: Alexander <kurokawa@tds.net>
> Date: Sat, 27 Jan 2007 20:17:54 -0600
> To: PHP-DB <php-db@lists.php.net>
> Subject: Re: [PHP-DB] Am I missing something?
>
> Alright, this may be the problem:
>
> Fatal error: Call to undefined function mysql_real_escape_string() in
> C:\apache2triad\htdocs\gallery.php on line 10
>
> which is refering to :
>
> $picture = mysql_real_escape_string($picture);
>
>
> /* never again use direct code from an example in a book, there's always an
> error */
>
> ----- Original Message -----
> From: "Alexander" <kurokawa@tds.net>
> To: "PHP-DB" <php-db@lists.php.net>
> Sent: Saturday, January 27, 2007 8:13 PM
> Subject: Re: [PHP-DB] Am I missing something?
>
>
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>

Niel Archer

2007-01-27, 9:58 pm

Hi

you need to change this:

> $insert = 'INSERT INTO domains ( picture, thumbnail ) values( "$picture",
> "$thumbnail")';


to be:
$insert = "INSERT INTO domains ( picture, thumbnail ) values( '$picture',
'$thumbnail')";

variables are only interpreted inside double quoted strings. Within
single quotes, everything is taken exactly as it appears.


Niel
Alexander

2007-01-27, 9:58 pm

I'm using PHP 5, I'm running this off of my personal laptop so I downloaded
ApacheTriad2
----- Original Message -----
From: "Stephen Johnson" <maillists@thelonecoder.com>
To: "Alexander" <kurokawa@tds.net>; "PHP-DB" <php-db@lists.php.net>
Sent: Saturday, January 27, 2007 8:24 PM
Subject: Re: [PHP-DB] Am I missing something?


> What version of php are using? It is odd that mysql_real_escape_string is
> undefined..
>
> Alternately you could addslashes...
> Hope this helps
>
> <?php
> /*
>
> Stephen Johnson c | eh
> The Lone Coder
>
> http://www.ouradoptionblog.com
> Join our journey of adoption
>
> http://www.thelonecoder.com
> stephen@thelonecoder.com
>
> continuing the struggle against bad code
>
> */
> ?>
>
>
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>

Alexander

2007-01-27, 9:58 pm

Alright, I did that. I now get :
INSERT INTO domains ( picture, thumbnail ) values( '', '')

and no data entered.

/* never take a hiatus from programming for a year */

----- Original Message -----
From: "Niel Archer" <niel@catweasel.org>
To: <php-db@lists.php.net>
Sent: Saturday, January 27, 2007 8:29 PM
Subject: Re: [PHP-DB] Am I missing something?


> Hi
>
> you need to change this:
>
>
> to be:
> $insert = "INSERT INTO domains ( picture, thumbnail ) values( '$picture',
> '$thumbnail')";
>
> variables are only interpreted inside double quoted strings. Within
> single quotes, everything is taken exactly as it appears.
>
>
> Niel
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>

Niel Archer

2007-01-27, 9:58 pm

Hi

> Alright, I did that. I now get :
> INSERT INTO domains ( picture, thumbnail ) values( '', '')
>
> and no data entered.


Then for some reason your variables are empty. Make sure they have data
on entry to the function. If they don't keep backtracking until you
find where it's lost.

Niel
Alexander

2007-01-28, 3:58 am

They should have data. here's the form code:

function write_form() {
echo "<form method='post' action='{$_SERVER['PHP_SELF']}'>";

echo "<p>Enter url of large picture: <input type='text'
name='picture' />";

echo "<p>Enter url of thumbnail: <input type='text'
name='thumbnail' />";

echo "<p><input type='submit' value='Add to Database' />";

echo "</form>";
}

I've done everything excatly how my PHP book says to /* i now officially
hate all code examples in books, they never work */
----- Original Message -----
From: "Niel Archer" <niel@catweasel.org>
To: <php-db@lists.php.net>
Sent: Saturday, January 27, 2007 9:07 PM
Subject: Re: [PHP-DB] Am I missing something?


> Hi
>
>
> Then for some reason your variables are empty. Make sure they have data
> on entry to the function. If they don't keep backtracking until you
> find where it's lost.
>
> Niel
>
> --
> PHP Database Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>

Stut

2007-01-28, 6:58 pm

Alexander wrote:
> They should have data. here's the form code:
>
> function write_form() {
> echo "<form method='post' action='{$_SERVER['PHP_SELF']}'>";
>
> echo "<p>Enter url of large picture: <input type='text'
> name='picture' />";
>
> echo "<p>Enter url of thumbnail: <input type='text'
> name='thumbnail' />";
>
> echo "<p><input type='submit' value='Add to Database' />";
>
> echo "</form>";
> }
>
> I've done everything excatly how my PHP book says to /* i now officially
> hate all code examples in books, they never work */


The book is probably expecting you to have register_globals enabled. DO
NOT ENABLE IT! Instead, read the following and adapt what the book is
giving you to work without it. Reasons for not enabling register_globals
are also given on this page...

http://php.net/register_globals

-Stut
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com