الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

مشكلة في اضافة الخبر

بدأه anasalqam في 17 نوفمبر 2011 · 2 رد · 433 مشاهدة · في منتدى تطوير المواقع بـ PHP
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

مرحبا

ارجو مساعدتي في حل مشكلة الخطا في السطر 2 من ملف :add.php

يمكن تحميل الاكواد من هنا

هذا بيانات تكون جدول قاعدة البيانات phpmyadmin:

CREATE TABLE `news` (
`id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`title` VARCHAR( 99 ) NOT NULL ,
`text` VARCHAR( 255 ) NOT NULL ,
`catimage` VARCHAR( 99 ) NOT NULL ,
`path` VARCHAR( 99 ) NOT NULL 
) ENGINE = MYISAM ;

والخطا في هذا الملف : add.php

<?php
IF ($action=="add"){
if ($_SERVER["REQUEST_METHOD"]=="POST")
{
$title      = addslashes($_POST['title']);
$title      = trim($_POST['title']);
$text       = addslashes($_POST['message']);
$text       = trim($text);

$file_name = $HTTP_POST_FILES['image']['name'];

$random_digit=rand(0000,9999);

$new_file_name=$random_digit.$file_name;

if ($title =="" || $text =="" || $file_name =="") {
echo "<BR><BR><BR><center><a href='javascript:history.back()'>One of the empty data</a><BR><BR><BR>";


}else{    

include("config.php");
$path= "upload/".$new_file_name;
if($image !=none)
{
if(copy($HTTP_POST_FILES['image']['tmp_name'], $path))
{

$catimage  =  $new_file_name;


$query = @mysql_query ("INSERT INTO news (title,text,catimage,path) VALUES ('$title','$text','$catimage','$path')") or die ("error query");

if($query){
echo "<BR>";    
echo "<CENTER><B>News has been added successfully";
echo "<META HTTP-EQUIV='Refresh' CONTENT='1;URL=news.php'>";
echo "<BR>";
}else{
echo "<BR>Sorry no information is entered into the table";

EXIT;
}
}
}
}
}

}else{
echo'<br>    
<form method="POST" enctype="multipart/form-data" action="news.php?action=add" dir="rtl">
<div align="center">
<table border="1" cellspacing="1" width="450" dir="rtl" id="table1" bordercolorlight="#C0C0C0" bordercolordark="#C0C0C0">
        <tr>
            <td class="bigfont" width="100" align="center"><span lang="ar-sa">
            Title</span></td>
            <td>

            <input type="text" name="title" id="title" size="20">

            </td>
        </tr>
        <tr>
            <td class="bigfont" width="100" align="center"><span lang="ar-sa">
            News</span></td>
            <td>

            <textarea rows="9" name="message" id="message" cols="29"></textarea></td>
        </tr>
        <tr>
            <td class="bigfont" width="100" align="center">Image with the news</td>
            <td>

            <input type="file" name="image" id="image" size="20">

            </td>
        </tr>
    </table>
</div>
<p align="center"><BR>
<?php
mysql_free_result($news);
?><title>add news</title>
<input type="submit" value="Send" name="B1">
<input type="reset" value="Clear" name="B2">
</p>
</form>';
}

ارجو المساعدة

#2

تم تصحيح add.php وجربته ويعمل بشكل جيد شرط اعطاء الصلاحيات المناسبة للمجلد upload للتمكن من الكتابة فيه

<?php

if (isset($_POST['action']) && $_POST['action'] == "Add") {
	if ($_SERVER["REQUEST_METHOD"]=="POST") {
		$title = addslashes($_POST['title']);
		$title = trim($title);
		$text  = addslashes($_POST['message']);
		$text  = trim($text);

		$file_name = $_FILES['image']['name'];

		if ($title == "" || $text == "" || $file_name == "") {
			echo "<br /><br /><br /><center><a href='javascript:history.back()'>One of the data is empty</a><br /><br /><br />";
		} else {
			include("config.php");

			$random_digit = rand(0000,9999);
			$file_name = $random_digit.$file_name;
			$path = "upload/".$file_name;

			if(copy($_FILES['image']['tmp_name'], $path)) {
				$catimage = $file_name;
				$query = @mysql_query ("INSERT INTO news (title,text,catimage,path) VALUES ('$title','$text','$catimage','$path')") or die ("error query");
				if($query) {
					echo "<br />";
					echo "<CENTER><B>News has been added successfully";
					echo "<META HTTP-EQUIV='Refresh' CONTENT='1;URL=news.php'>";
					echo "<br />";
				} else {
					echo "<br />Sorry no information is entered into the table";
					exit();
				}
			}
		}
	}
} else {
?>
<br />
	<form method="post" enctype="multipart/form-data" action="add.php" dir="rtl">
		<div align="center">
			<table border="1" cellspacing="1" width="450" dir="rtl" id="table1" bordercolorlight="#C0C0C0" bordercolordark="#C0C0C0">
				<tr>
					<td class="bigfont" width="100" align="center"><span lang="ar-sa">Title</span></td>
					<td><input type="text" name="title" id="title" size="20"></td>
				</tr>
				<tr>
					<td class="bigfont" width="100" align="center"><span lang="ar-sa">News</span></td>
					<td><textarea rows="9" name="message" id="message" cols="29"></textarea></td>
				</tr>
				<tr>
					<td class="bigfont" width="100" align="center">Image with the news</td>
					<td><input type="file" name="image" id="image" size="20"></td>
				</tr>
			</table>
		</div>
		<p align="center">
			<title>add news</title>
			<input type="submit" value="Add" name="action">
			<input type="reset" value="Clear">
		</p>
	</form>
<?php } ?>

سؤالي هو لماذا استعملت الحقل catimage وما هو دوره اذا كان مسار الصورة محفوظا في الحقل path ؟

يمكن الغاء الشرط في السطر 4

if ($_SERVER["REQUEST_METHOD"]=="POST") {

تم تعديل هذه المشاركة بواسطة choclatboy في 18 نوفمبر 2011 في 01:06

#3

أولا لم يتم تعريف المتغير action$ قبل الجملة الشرطية

if ($action=="add")

ثانيا تصحيح الصفحة التي يرسل اليها النموذج

في هذا السطر لاوجود أصلا للصفحة news.php

<form method="POST" enctype="multipart/form-data" action="news.php?action=add" dir="rtl">

تم التصحيح ليصبح:

<form method="post" enctype="multipart/form-data" action="add.php" dir="rtl">

ثالثا: للتأكد ان المستخدم قد ضغط على الزر Send في النموذج Form يجب تعديل هذا الزر ليصبح

<input type="submit" value="Add" name="action">

القيمتين المهمتين هما name و value

في صفحة php نستعمل الشرطين

if (isset($_POST['action']) && $_POST['action'] == "Add")

ومن تم يمكن الاستغناء عن الشرط

if ($_SERVER["REQUEST_METHOD"]=="POST")

رابعا

تم استبدال HTTP_POST_FILES_$ بـ FILES_$ ﻷنها لم تعد معتمدة

خامسا: استعملت نفس المتغير file_name$ بدل استعمال متغير جديد

$file_name = $random_digit.$file_name;
$path = "upload/".$file_name;

سادسا تصحيح الاسطر

$title = addslashes($_POST['title']);
$title = trim($_POST['title']);

لتصبح

$title = addslashes($_POST['title']);
$title = trim($title);
1

مواضيع مشابهة