Forum Moderators: coopster

Message Too Old, No Replies

Not a Valid MySQL-Link Resource.

I'm confused now. It's not my connections...

         

pantheros

11:32 pm on Dec 8, 2004 (gmt 0)

10+ Year Member



Okay. I'm opening a graphics site, and i'm trying to create a form that will automatically generate a table in my layouts page. I'm fairly new to PHP, but i've been getting along fairly well so far.

The error i'm getting is: "Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/panthero/public_html/gentry/submitlayout.php on line 7".

I'll post the code from the pages i'm having trouble with so that you may be able to understand what the problem is better. My site is currently skinned as well. Also, <password> is were my real password is.

submitlayout.php:


<?php

$dbh=mysql_connect ("localhost", "panthero_panther", "<password>") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("panthero_pantherodb");

$q="insert into layouts (id,name,creator,creatorurl,date,series,category,previewimage,fileurl,previewurl,describe) VALUES ('','$filename','$creator','$creatorurl','$date','$series','$category','$previewimage','$previewurl','$describe')";
$result = mysql_query($q,$connection);
if ($result)
{
echo "thank you, Layout has been added.";
}
?>

Any ideas what the problem may be? I'd be much appreciative of any help i can get from anyone.

[edited by: jatar_k at 7:03 am (utc) on Dec. 9, 2004]
[edit reason] removed unnecessary code [/edit]

jatar_k

12:30 am on Dec 9, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld pantheros,

holy thats a lot of code and frankly I didnt read it.

I read the error message and went to the line it mentioned, I am not 100% sure what the actual line is but I have an educated hunch

I am guessing this line is dying
$result = mysql_query($q,$connection);

reason being that I don't see reference to a var named $connection since I see this

$dbh=mysql_connect ("localhost", "panthero_panther", "<password>") or die ('I cannot connect to the database because: ' . mysql_error());

your mysql_query should probably like so

$result = mysql_query($q,$dbh);

though since the second param is optional you could just have

$result = mysql_query($q);

pantheros

12:55 am on Dec 9, 2004 (gmt 0)

10+ Year Member



Thanks for the help and the welcome. This is the first PHP help site that has actually responded to my questions. The error is no longer there, but now it's not doing the writing. I've changed the code to:


<?php

$dbh=mysql_connect ("localhost", "panthero_panther", "<password>") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("panthero_pantherodb");

$q = "INSERT INTO layouts (id, name, creator, creatorurl, date, series, category, previewimage, fileurl, previewurl, describe) VALUES ('', '$filename', '$creator', '$creatorurl', '$date', '$series', '$category', '$previewimage', '$previewurl', '$describe')";
$result = mysql_query($q);
if ($result)
{
echo "Thankyou. The layout has been added. Please press the BACK button.";
}
?>

It doesn't echo, and it isn't writing to the database. Any ideas why it isn't writing to the database?

jatar_k

1:09 am on Dec 9, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



ok then, next step

your query could be dying, add this

$result = mysql_query($q) or die (mysql_error());

and see if there is an error being returned from mysql

pantheros

1:13 am on Dec 9, 2004 (gmt 0)

10+ Year Member



I added the code you gave me instead of the previous $result and all it showed was "Query was empty". I filled out the form enitrely and tried it again, and i still got "Query was empty".

jatar_k

4:36 am on Dec 9, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



then add this right before it and output your qeury to the browser

echo '<p>',$q;
die();
$result = mysql_query($q) or die (mysql_error());

that will output the actual contents of the $q variable and then kill the script

pantheros

6:10 am on Dec 9, 2004 (gmt 0)

10+ Year Member



That still did not work. I'm still getting a blank page that is not writing to my database. What can i do? I changed the code that you gave me to the following, which i had in another page:


$result= mysql_query($q) or die
("Could not execute query : $q." . mysql_error());

Now it's telling me it can't INSERT INTO layouts "$q here". Then it says "Column count doesn't match value count at row 1". I'm very confused now. :(

jatar_k

6:24 am on Dec 9, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



well I ran exactly this

<?
$q = "INSERT INTO layouts (id, name, creator, creatorurl, date, series, category, previewimage, fileurl, previewurl, describe) VALUES ('', '$filename', '$creator', '$creatorurl', '$date', '$series', '$category', '$previewimage', '$previewurl', '$describe')";
echo '<p>1:',$q;
?>

and my output was

1:INSERT INTO layouts (id, name, creator, creatorurl, date, series, category, previewimage, fileurl, previewurl, describe) VALUES ('', '', '', '', '', '', '', '', '', '')

which makes sense since I didn't have the vars

if there was no output and from the error message $q is not being evaluated for some reason. I can't see why.

pantheros

6:35 am on Dec 9, 2004 (gmt 0)

10+ Year Member



Any ideas of how i could rewrite it so that it WILL work and write into my table?

jatar_k

6:41 am on Dec 9, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



gimme a few minutes I'll crank up apache et al, build a few tables and see if I can figure out where the error is

pantheros

6:46 am on Dec 9, 2004 (gmt 0)

10+ Year Member



Okie dokie. Thanks a lot for your help.

jatar_k

7:06 am on Dec 9, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



alright I found a few things

You can't have a column named 'describe' it's a reserved word, I changed it to description (that's up to you)

You also missed a variable in your insert, $fileurl, you have it in the column list but not in the variables

I used this create statement, I just made it up

create table layouts(
id int(4) auto_increment primary key,
name varchar(100),
creator varchar(100),
creatorurl varchar(255),
date varchar(100),
series varchar(100),
category varchar(100),
previewimage varchar(255),
fileurl varchar(255),
previewurl varchar(255),
description varchar(100));

and I used this code in the end

<?
$filename = 'somefile.php';
$creator = 'jatar_k';
$creatorurl = 'someotherurl.com';
$date = '08/12/2004';
$series = 'someseries';
$category = 'somecategory';
$previewimage = '/somejpg.jpg';
$fileurl = 'some/other/url';
$previewurl = 'url.com';
$describe = 'this is going to work i hope';

//$dbconn = mysql_connect('localhost','myuser','mypass') or die (mysql_error());

$dbh=mysql_connect('localhost','myuser','mypass') or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("panthero_pantherodb");

$query = "INSERT INTO layouts (id, name, creator, creatorurl, date, series, category, previewimage, fileurl, previewurl, description) VALUES ('', '$filename', '$creator', '$creatorurl', '$date', '$series', '$category', '$previewimage', '$fileurl', '$previewurl', '$describe')";

$result = mysql_query($query) or die ('<p>err: ' . mysql_error());
if ($result) echo "Thankyou. The layout has been added. Please press the BACK button.";
?>

that should fix it :)

pantheros

7:28 am on Dec 9, 2004 (gmt 0)

10+ Year Member



Okay, so rather than using the values you've given, how can i get it to added the variables from the form on the page it will be linked to? The file currently looks like so:


<?
$filename = 'filename';
$creator = 'creator';
$creatorurl = 'creatorurl';
$date = 'date';
$series = 'series';
$category = 'category';
$previewimage = 'previewimage';
$fileurl = 'fileurl';
$previewurl = 'previewurl';
$describe = 'description';

$dbh=mysql_connect ("localhost", "panthero_panther", "<password>") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("panthero_pantherodb");

$query = "INSERT INTO layouts (id, filename, creator, creatorurl, date, series, category, previewimage, fileurl, previewurl, description) VALUES ('', '$filename', '$creator', '$creatorurl', '$date', '$series', '$category', '$previewimage', '$fileurl', '$previewurl', '$describe')";

$result = mysql_query($query) or die ('<p>err: ' . mysql_error());
if ($result) echo "Thankyou. The layout has been added. Please press the BACK button.";
?>

It IS writing to the database now, just not the input from the form.

jatar_k

8:19 am on Dec 9, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



just remove these lines then (all the test data)

$filename = 'filename';
$creator = 'creator';
$creatorurl = 'creatorurl';
$date = 'date';
$series = 'series';
$category = 'category';
$previewimage = 'previewimage';
$fileurl = 'fileurl';
$previewurl = 'previewurl';
$describe = 'description';

and you should be off to the races

and I am going to bed, back tomorrow ;)

pantheros

8:51 am on Dec 9, 2004 (gmt 0)

10+ Year Member



Woohoo! It works now! Thank you sooooo much for taking time out to help me. I've been asking around for help on things like these since september with no luck. I'll have to haunt these forums now i suppose. lol

jatar_k

5:51 pm on Dec 9, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



another good hint, check out the PHP library [webmasterworld.com] we have, there are some good threads about basic mysql, among other things.

very happy that my last post before bed sorted it all, I have tendency to say stupid things at that time. ;)