Hi Guys,
I’m trying to create a system whereby when I create a news story I can also upload a picture with the news story.
That picture should be saved in a ‘pics’ folder at the same level as this actual script in the php page and the folder has been CHMOD 0777.
Trouble is, when I fill in all the details and hit submit all the page does is place the path of where I got the picture from ie C:\Documents and Settings\My NameDesktop\picture.jpg at the top of the screen.
Now what it should do when I hit submit is save the picture in the folder. It does however save all the field records I added in MySQL database in my ‘news’ table, but does not show the message ‘News Record is updated’ and saves the picture path to the desktop as shown above.
Also when I go back in and I want to edit my records. It throws up this:
Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in /home/sleaz/public_html/news/add_news.php on line 73
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/sleaz/public_html/news/add_news.php on line 74
Which are these two lines:
$result = mysql_query("Select * From news where newsid=".$_GET['newsid'],$link);
$row = mysql_fetch_array($result, MYSQL_BOTH);
So can anyone please help as of to why:
A) It’s saving the path to the desktop in the ‘news’ table and not saving the image in the ‘pics’ folder
😎 When I go and edit a record its throwing up the above code?
I’ve included my database ‘news’ table structure and the code below
Can someone please help!
Thank you!!!
newsid int(3) No auto_increment
name varchar(255) No
picture varchar(255) No
headline varchar(255) No
storydate varchar(255) No
byline varchar(255) No
opening text No
body text No
notes varchar(255)
<html>
<head>
<title>Add News Story</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="../ssheet.css">
</head>
<body>
<?php
// include files to log in
include("../*****.php");
include("../******.php");
// action variable to either save or display uploaded file
if($action == "" || $action == "form"){ loadForm(); }
elseif($action == "upload"){ uploadFile(); }
// if the action is set to upload, we launch this function
function uploadFile(){
// get get the variable names from the form, all we have to do is 'globalize' them"
global $name, $headline, $storydate, $byline, $picture, $opening, $body, $notes;
// this will copy the picture into my upload directory
copy($HTTP_POST_FILES['picture']['tmp_name'],"pics/".$HTTP_POST_FILES['picture']['name']);
// Echo the pic to make sure it works.
echo $picture;
// Run inserting and updating variables code
if(!isset($_GET['newsid'])) {
$result = mysql_query("Insert into news(name,headline,storydate,byline,picture,opening,body,notes) values('$name','$headline','$storydate','$byline','$picture','$opening','$body','$notes')");
$msg = "New record is saved";
} else {
$result = mysql_query("Update news set name='$name', headline='$headline', storydate='$storydate', byline='$byline', picture='$picture', opening='$opening', body='$body',notes='$notes' where newsid=".$_GET['newsid']);
$msg = "News Record is updated";
}
// when we are done saving and / or updating
loadForm();
} // end of function uploadFile
// if the action varibale is blank or set to 'form' we are going to diplay the form
function loadForm(){
// Get the variable names for each id
if(isset($_GET['newsid'])) {
$result = mysql_query("Select * From news where newsid=".$_GET['newsid'],$link);
$row = mysql_fetch_array($result, MYSQL_BOTH);
$name = $row['name'];
$headline = $row['headline'];
$storydate = $row['storydate'];
$byline = $row['byline'];
$picture = $row['picture'];
$opening = $row['opening'];
$body = $row['body'];
$notes = $row['notes'];
}
if(mysql_error())
{
echo mysql_error() ."<br>\n";
}
?>
<form id="uploadForm" action="add_news.php" method="post">
<table width="100%" align="center" cellpadding="1" cellspacing="1" height="588">
<tr bgcolor="#CC0000">
<td colspan="3" height="20"> <span class="yellow_text">ADD NEWS:</span>
<span class="white_text"><b>Please Add Full Details</b></span> </td>
</tr>
<tr>
<td colspan="3" bgcolor="#FFFF00"><?php echo $msg?></td>
</tr>
<tr background="../admin/lines.gif">
<td colspan="3" height="1"></td>
</tr>
<tr>
<td width="7%">
<div align="center"><img src="../admin/images/1.jpg" width="20" height="20"></div>
</td>
<td width="29%" class="bold">Name of Author:</td>
<td width="64%">
<p>
<input name="name" type="text" id="name" value="<?php echo $name?>" size="36">
</p>
</td>
</tr>
<tr>
<td width="7%" height="25">
<div align="center"><img src="../admin/images/2.jpg" width="20" height="20"></div>
</td>
<td width="29%" class="bold" height="25">Headline:</td>
<td width="64%" height="25">
<input name="headline" type="text" id="headline" value="<?php echo $headline?>" size="36">
</td>
</tr>
<tr>
<td width="7%">
<div align="center"><img src="../admin/images/3.jpg" width="20" height="20"></div>
</td>
<td width="29%" class="bold">Date:</td>
<td width="64%">
<input name="storydate" type="text" id="storydate" value="<?php echo $storydate?>" size="36">
</td>
</tr>
<tr>
<td width="7%">
<div align="center"><img src="../admin/images/4.jpg" width="20" height="20"></div>
</td>
<td width="29%" class="bold">Byline:</td>
<td width="64%">
<select class="input" name="byline" id="byline">
<option value="Andy Fitzsimons"<? if ($byline == 'Andy Fitzsimons') echo ' selected' ?>>Andy
Fitzsimons
</select>
</td>
</tr>
<tr>
<td width="7%"> </td>
<td width="29%" class="bold">Picture</td>
<td width="64%">
<input type="file" name="picture" size="20">
</td>
</tr>
<tr>
<td width="7%" valign="top">
<div align="center"><img src="../admin/images/4.jpg" width="20" height="20"></div>
</td>
<td width="29%" class="bold" valign="top">Opening:</td>
<td width="64%">
<textarea name="opening" id="opening" cols="60" rows="7"><?php echo $opening?></textarea>
</td>
</tr>
<tr>
<td width="7%" valign="top">
<div align="center"><img src="../admin/images/5.jpg" width="20" height="20"></div>
</td>
<td width="29%" class="bold" valign="top">Body:</td>
<td width="64%">
<textarea name="body" id="body" cols="60" rows="20"><?php echo $body?></textarea>
</td>
</tr>
<tr>
<td width="7%" valign="top" height="86">
<div align="left"><img src="../admin/images/7.jpg" width="20" height="20"></div>
</td>
<td width="29%" valign="top" height="86"> <span class="bold">Additional
Reference Notes:</span><br>
<span class="light_blue">Not Published, used as reference at a later date.</span></td>
<td width="64%" height="86">
<textarea name="notes" id="notes" cols="60" rows="3"><?php echo $notes?></textarea>
</td>
</tr>
<tr valign="top">
<td colspan="3" height="3" background="../images/lines.gif"></td>
</tr>
<tr>
<td colspan="3">
<p align="right">
<input type="submit" name="Submit" value="Submit" class="button" />
<input type="reset" name="Reset" value="Reset" class="button" />
<input type="hidden" name="action" value="upload" />
</p>
</td>
</tr>
</table>
</form>
<?php } // end of function load form ?>
<div align="center"></div>
</body>
</html>
News Table
newsid int(3) No auto_increment
name varchar(255) No
picture varchar(255) No
headline varchar(255) No
storydate varchar(255) No
byline varchar(255) No
opening text No
body text No
notes varchar(255)