Jump to content

Need help with basic PHP+MYSQL


particle

Recommended Posts

Hi all. First post here :)

 

I recently started web-programming again and now I want to make an admin system for text and images to support my sites.

But I have run into some trouble... alot :P

I hope some of you can help :)

 

 

I keep getting errors like Warning: "mysql_fetch_array(): supplied argument is not a valid MySQL result resource"

and "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[[0]]) VALUES ( '' )' at line 1 - ID - "

 

I have tried so many things now, and I just can't see where the fault is.

 

In my MySQL DB I have:

 

DB = web***_2

table = txt

and only 2 fields: "ipid" & "fronttxt1"

 

fronttxt1 - text - No - primary - unique - index

ipid - smallint(1) -No - Auto_increment

 

 

 

This is the page code:

 

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

 

<head>

<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />

<title>Admin Tools</title>

<base target="_self" />

<style type="text/css">

.style1 {

text-align: center;

font-family: "Eras Bold ITC";

color: #FFFFFF;

}

.style2 {

font-family: "Eras Bold ITC";

font-size: medium;

text-decoration: underline;

}

</style>

</head>

 

<body style="background-color: #000000">

<?php

// variables

$sqlhost = 'db.******ructures.dk';

$sqluser = 'web****';

$sqlpass = '1******';

$sqldb = 'web****_2';

$sqltxt = 'txt';

$sqlimg = 'img';

$sqlnews = 'news';

$sqladmin = 'admin';

$ftxt1 = 'fronttxt1';

$ftxt2 = 'fronttxt2';

$ftxt3 = 'fronttxt3';

$sub11 = 'sub1txt1';

$sub12 = 'sub1txt2';

$sub21 = 'sub2txt1';

$sub22 = 'sub2txt2';

$sub31 = 'sub3txt1';

$sub32 = 'sub3txt2';

$sqltxt = 'txt';

$cdb = mysql_connect("$sqlhost", "$sqluser", "$sqlpass");

// connection

mysql_connect("$sqlhost", "$sqluser", "$sqlpass");

mysql_select_db("$sqldb");

// Ask for txt data

echo "mysql_error()";

 

?>

 

<font color="FFFFFF">

 

<table align="center" style="width: 700px">

<tr>

<td>

<table align="center" style="width: 100%">

<tr>

<td class="style1" valign="top">Frontpage text 1</td>

</tr>

<tr>

<td class="style1" valign="top">

<form action="front1txt.php" method="post"><td/></form>

<?php

// ----- ID FETCH ----- //

$iddel = "DELETE ipid FROM txt";

mysql_query( $iddel );

$randid = Rand(1, 9);

$newid = "INSERT INTO txt ('ipid')  VALUES ('$randid')";

echo "mysql_error()";

echo "SQLDB - $sqldb";

?>

<?php

$gettxt = "SELECT fronttxt1 FROM txt";

// mysql_select_db('$sqldb');

 

$rettxt = mysql_query( $gettxt );

if (! $rettxt )

{

echo "mysql_error()";

}

while($rettxt = mysql_fetch_array($rettxt))

{

echo "mysql_error()";

}

?>

<form name="txtare"><textarea name="input" style="width: 700px; height: 300px"><?php echo STRIPSLASHES(TRIM(nl2br($rettxt[0]))); ?></textarea>

<tr/>

<tr>

<td class="style1" valign="top">

 

<input type="submit" style2 name="submit" style="width: 500px" type="submit" value="Save Changes" /><form/>

</td>

</tr>

</table>

</td>

</tr>

</table>

<?php

// --- Submit ----

if (isset($_POST['submit'])) {

    $sq1 = "SELECT * FROM fronttxt1";

    $txtresult = mysql_query($sq1);

echo "mysql_error()";

}

 

$txtsql = array();

$txtsql['input'] = mysql_real_escape_string($_POST['input']);

$txtsq = "INSERT INTO txt (fronttxt1[[0]]) VALUES ( '{$txtsql['input']}' )";

mysql_query($txtsq);

echo mysql_error();

echo "<h1>Your information has been entered into the database</h1>";

 

echo mysql_error();

echo " - ID - $id";

echo "<br>";

echo " - txtsq - $txtsq";

echo "<br>";

echo " -  - $";

echo "<br>";

echo "txtoutput - $txtoutput";

 

?>

 

</font>

</body>

 

</html>

Link to comment
https://forums.phpfreaks.com/topic/170431-need-help-with-basic-phpmysql/
Share on other sites

$cdb = mysql_connect("$sqlhost", "$sqluser", "$sqlpass");
// connection
mysql_connect("$sqlhost", "$sqluser", "$sqlpass");

 

should be:

 

$cdb = mysql_connect($sqlhost, $sqluser, $sqlpass);

 

If you want to create an additional connection:

 

$cdb2 = mysql_connect($sqlhost, $sqluser, $sqlpass, true/*$new_link*/);

 

$iddel = "DELETE ipid FROM txt";

 

is wrong and should be:

 

$iddel = "DELETE FROM txt";

 

This will delete all rows from txt

 

echo "mysql_error()";

 

does work (because of the complex string) but should be:

 

echo mysql_error();

 

while($rettxt = mysql_fetch_array($rettxt))

holds to much (both numerical and associative) use mysql_fetch_assoc() instead

 

while($rettxt = mysql_fetch_array($rettxt))
{
echo "mysql_error()";
}

does nothing if you want to store the result, use:
$data = array();
while($rettxt = mysql_fetch_assoc($rettxt)) {
    $data[] = $rettxt;
}

 

STRIPSLASHES(TRIM(nl2br($rettxt[0])))

 

altough this works it's best to write it lowercase

 

$txtsql['input'] = mysql_real_escape_string($_POST['input']);

should be within the if (isset($_POST['submit'])) part or will throw errors otherwise

 

$txtsq = "INSERT INTO txt (fronttxt1[[0]]) VALUES ( '{$txtsql['input']}' )";

is invalid and should be:

 

$fonttxt1 = mysql_fetch_assoc($txtresult);
$txtsq = "INSERT INTO txt (fronttxt1[[0]]) VALUES ( '{$txtsql['input']}' )";

WOW!!! That was freaking awsome... :D

You jusst saved my day :)

 

But not all is good...

I still can't get rid of this:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /usr/home/web/web****/***/front1txt.php on line 54

:facewall:

 

And

<textarea name="input" style="width: 700px; height: 300px"><?php echo striplashes(trim(nl2br($rettxt[0]))); ?></textarea>
Won't parse any info to the textarea :(

But not all is good...

I still can't get rid of this:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /usr/home/web/web****/***/front1txt.php on line 54

 

before you pass it to fetch_assoc() (not array if you only need the associative part) check the result first:

 

if ($result && mysql_num_rows($result))

 

And

<textarea name="input" style="width: 700px; height: 300px"><?php echo striplashes(trim(nl2br($rettxt[0]))); ?></textarea>
Won't parse any info to the textarea :(

 

If you are using my code then yes this fails: $rettxt[0] change 0 to the column name.

hehe... I must have pasted right after I quit last night, cause it was first after several hours that I began to put other stuff in the code.

 

Yeah, I have no idea why it does like that. It's ever fetch_row and fetch_array etc...

I am starting to think that it could have something to do with my MySQL server... It's only version 3.1.2...

And also I was setting up a forum on one of my domains last night and it just kept doing strange things... So I really think there is something with my SQL server... But it's sunday and the guys at the hosting firm never work weekends :(

 

But I think I'll find some debugging tools and some way to check the connection to the server... :)

Then tomorrow I can continue the project :D

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.