Jump to content

Need help with really simple code...


blurrydude

Recommended Posts

it's late, I'm tired, and i'm not the worlds greatest coder...

i want to create a script that allows me to use a form to update text content on another page.

it only needs to pull text out of the database, but the text is going to be long. i've tried the following:

 

---dbinfo.inc.php:

<?

$username="test2";

$password="tset ";

$database="testdb_test2";

?>

 

update.php

<?

include("dbinfo.inc.php");

mysql_connect(localhost,$username,$password);

@mysql_select_db($database) or die( "Unable to select database");

$query="SELECT * FROM biotext";

$result=mysql_query($query);

 

mysql_close();

 

echo $result;

 

 

?>

 

<form action="updated.php">

<input type="hidden" name="ud_id">

Bio: <input type="text" name="ud_first" value="<? echo "$first"?>"><br>

<input type="Submit" value="Update">

</form>

 

---updated.php:

<?

include("dbinfo.inc.php");

mysql_connect(localhost,$username,$password);

 

$query="UPDATE biotext SET biotext='$ud_first'";

@mysql_select_db($database) or die( "Unable to select database");

mysql_query($query);

echo "Record Updated";

echo $ud_first;

mysql_close();

?>

 

It accesses the database, doesn't throw any errors, then it returns blank data.

and the echo $ud_first; also returns blank. I created the table in the database with this, just as a test,

i know the varchar (15) is contradictive to my earlier statement about long text:

 

<?

include("dbinfo.inc.php");

mysql_connect(localhost,$username,$password);

@mysql_select_db($database) or die( "Unable to select database");

$query="CREATE TABLE biotext (biotext varchar(15) NOT NULL)";

mysql_query($query);

mysql_close();

echo "Database created";

?>

 

what the hell am i doing wrong.

I want:

 

form field to database entry (same entry everytime)

database entry to form field for the update page (same entry again)

 

that's it. I can pull from a text file nice and easy with this:

 

<?php

$file = 'bio.txt';

$f = fopen($file, r);

while ( $line = fgets($f, 1000) ) {

print $line;

}

?>

 

in my html, but i can't save to a file because the server wont allow it, at least not this way:

 

---edit.php:

 

<html><body>

<h4>Test file write form</h4>

<form action="process.php" method="post">

Enter Text:<br>

<textarea name="bio" rows="22" cols="54">

<?php

$file = 'bio.txt';

$f = fopen($file, r);

while ( $line = fgets($f, 1000) ) {

print $line;

}

?>

</textarea>

<input type="submit" />

</form>

</body></html>

 

and this...

 

---process.php:

 

<html><body>

<?php

$bio = $_POST['bio'];

 

echo "Your text has been updated";

 

$myFile = "bio.txt";

$fh = fopen($myFile, w) or die("can't open file");

$stringData = $bio;

fwrite($fh, $stringData);

fclose($fh);

 

?>

</body></html>

 

It returns with a php warning that access is denied to the file. excuse the crudity of the scripts,

they're research. i think the server is set up not to allow the function.

Link to comment
https://forums.phpfreaks.com/topic/89032-need-help-with-really-simple-code/
Share on other sites

I tried it this way once:

 

<?

include("dbinfo.inc.php");

mysql_connect(localhost,$username,$password);

@mysql_select_db($database) or die( "Unable to select database");

$query="SELECT * FROM biotext";

$result=mysql_query($query);

 

$first=mysql_result($result,"biotext");

 

mysql_close();

 

echo "<b><center>Database Output</center></b><br><br>";

 

?>

 

<font face="Arial, Helvetica, sans-serif"><? echo "$first"; ?></font>

 

but I got this back:

 

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /index.php on line 8

 

Database Output

You've made really obvious mistakes that would be easily remedied if you looked at the php manual for the functions you're using.  Your code assumes along the way that things worked, and they probably didn't.  Without checking, you won't have any information to help you narrow down the problem.

 

How about this for a push in the right direction:

 

include("dbinfo.inc.php");
$link = mysql_connect(localhost, $username, $password);
if (!$link) {
    die('Could not connect: ' . mysql_error());
}
mysql_select_db($database) or die( "Unable to select database");
$query = "SELECT * FROM biotext";
$result = mysql_query($query);
if (!$result) {
    die('Invalid query: ' . mysql_error());
}

while ($row = mysql_fetch_assoc($result)) {
  print_r($row);
}


 

 

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.