PDA

View Full Version : insert data with php [was So Simple I'm Almost Embarrased To Post]


Scriptbanger
10-21-2006, 12:20 AM
:o Okay, it's been awhile but I shouldn't have forgotten the basics. Hopefully I am posting in the right section.

I have a very simple form [4 fields] and I need to put that data into a database using PHP and MySQL. And all I am getting is an error. Please, somebody tell me what I have done wrong. I am following a tutorial and have done everything like I was shown.

Here is the error I am getting: Warning: mysql_select_db (http://php.net/mysql_select_db)(): supplied argument is not a valid MySQL-Link resource in . . .\media_interface.php on line 10


In the head section of my document:

<?php
$db_host = "****";
$db_user = "****";
$db_pwd = "****";
$db_name = "multimediadb";

mysql_connect (http://php.net/mysql_connect)($db_host, $db_user, $db_pwd);
mysql_select_db (http://php.net/mysql_select_db)($db_name);
?>



In the body of my form page:

<?php
if (!isset (http://php.net/isset)($_POST['submit'])) {
?>
<form name="mediaPost" id="mediaPost" action="" method="post">
<table align="center" cellspacing="5" cellpadding="10" border="0" class="noborder" style="background-color:#eeeeee;">
<tr><td class="border">
<b>Media Format</b><br>
<input type="radio" name="type" id="type" value="videolocal" class="radio"> Video <small>[FTP Local]</small> &nbsp; &nbsp; &nbsp;| &nbsp; &nbsp; &nbsp;
<input type="radio" name="type" id="type" value="videoyoutube" class="radio"> Video <small>[YouTube]</small> &nbsp; &nbsp; &nbsp;| &nbsp; &nbsp; &nbsp;
<input type="radio" name="type" id="type" value="slideshow" class="radio"> Slideshow &nbsp; &nbsp; &nbsp;| &nbsp; &nbsp; &nbsp;
<input type="radio" name="type" id="type" value="audio" class="radio"> Audio
</td></tr>
<tr><td class="border">
<b>File Location</b> <small>[Filename -OR- YouTube URL]</small><br />
<input type="text" name="filename" id="filename" style="width:600px;">
</td></tr>
<tr><td class="border">
<b>Descriptive Name</b> <small>[Short Description]</small><br />
<input type="text" name="name" id="name" style="width:600px;">
</td></tr>
<tr><td class="border">
<b>Caption</b> <small>[Long Text]</small><br />
<textarea name="caption" id="caption" style="width:600px; height:100px;"></textarea>
</td></tr></table>
<div style="font-size:8pt;">&nbsp;</div>
<div align="center"><input type="submit" value="Post Media" class="button"> &nbsp; &nbsp; <input type="reset" value="Reset Form" class="button"></div>
</form>
<?php
} else {
$filename = $_POST[‘filename’];
$name = $_POST[‘name’];
$caption = $_POST[‘caption’];
$type = $_POST[‘type’];

mysql_query (http://php.net/mysql_query)("INSERT INTO `multimediadb` (filename, name, caption, type) VALUES (‘$filename’, ‘$name’, ‘$caption’, ‘$type’)");
echo "Success! Your media post was successful! If you followed the instructions you should now be able to access your file.";
}
?>

GJay
10-21-2006, 12:38 AM
try:

mysql_connect($db_host, $db_user, $db_pwd) or die(mysql_error());



[edit]
as an aside, did you manually add the manual links, or is it magical?

Scriptbanger
10-21-2006, 12:46 AM
Oh, yeah. I remember that. Thanks GJay.

Didn't do anything though. No errors, nothing in the database.

Oh, I didn't build the database and I just found this out. I am trying to hit a specific table in the database named 'files'. I'm sure this makes a difference in the code but I don't know how to address it. Sorry for the ommission in my initial post.

CFMaBiSmAd
10-21-2006, 12:47 AM
The mysql_connect is probably failing. When the mysql_select_db function is called there is currently no active link to a mysql server.

Any function call can fail. You need to test the status that is returned and take appropriate action.

Try the following -
mysql_connect($db_host, $db_user, $db_pwd) or die('Could not connect to mysql server: '. mysql_error());
mysql_select_db($db_name) or die('Could not select database: '. mysql_error());

Scriptbanger
10-21-2006, 12:58 AM
CFMaBiSmAd, I put the code in and still nothing. The lack of errors is confusing.

Am I supposed to be accessing the table called 'files' in the database called 'multimediadb' and if so, how do I do that?

Scriptbanger
10-21-2006, 01:39 AM
Here is where it now stands. There was a lot of garbage in the way of this script working. I was using code from a tutorial and I noticed that a lot of the single quotes were actually special character glyphs and not the plain old '. Changed them out but there is another funny thing in the code.

In the code after the form there is an "}else{" reference. But there is nothing prior. There is no THIS or ELSE. When I try and remove it however, it echos the success message when the page loads. Can somebody help me get rid of, or complete the 'else'?

I will clean up the code and repost. If anyone has any ideas or knows of any glaring errors let me know and I'll get them into the rewrite.

Thanks for all your help. It got me farther along than when I first posted.

Fumigator
10-21-2006, 05:46 AM
Well actually when you put the code inside the <?php ?> tags, the } else { has an if statement that matches up with it-- it's on line 2 of the code you posted, the if(isset($_POST['submit'])) statement.

You can jump in and out of PHP code anytime you want, but the PHP processor will put it all together and code blocks have to be properly closed.