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

خطأ في كود اضافة سجل الى قاعدة البيانات!

بدأه سيد الاحساس في 25 أغسطس 2010 · 7 رد · 1,050 مشاهدة · في منتدى تطوير المواقع بـ PHP
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم ..

لدي صفحة لإضافة غرفة الى موقع فندق .. وعندما اقوم بتشغيل الكود .. تظهر لي صفحة الاضافة تمام بس يظهر لي خطأ عند اضافة الغرفة لقاعدة البيانات ..

unable to insert records: 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 'from ,to ,type ,floor ,price ,TV ,air_condition ,internet_access, booked ) VALU' at line 1

طبعاً فيه عندي 3 صفحات :

صفحة الاتصال بقاعدة البيانات config.php

<!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">

<?php

// define the server information
$db_server='localhost';
$db_user='root';
$db_pass='';
$db_name='abha_hotel';

// define a new function to use many methods like strip_tags(), addslashes() and trim()
function make_it_safe($string)
{
	$string = strip_tags(addslashes(trim($string)));
	return $string;
}

?>


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

<body>
</body>
</html>

فورم اضافة الغرف addnewroom.php

<?php 
session_start();
?>
<!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 http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>add new room</title>

<script src="SpryAssets/SpryValidationTextField.js" type="text/javascript"></script>
<script src="SpryAssets/SpryValidationSelect.js" type="text/javascript"></script>
<script src="SpryAssets/SpryValidationCheckbox.js" type="text/javascript"></script>
<link href="SpryAssets/SpryValidationTextField.css" rel="stylesheet" type="text/css" />
<link href="SpryAssets/SpryValidationSelect.css" rel="stylesheet" type="text/css" />
<link href="SpryAssets/SpryValidationCheckbox.css" rel="stylesheet" type="text/css" />
</head>

<body>
<form id="form1" name="form1" method="post" action="addroom_check.php">
  <table width="50%" border="1" align="center">
    <tr>
      <th scope="row"> </th>
      <td> </td>
    </tr>
    <tr>
      <th scope="row">Room type:</th>
      <td><span id="spryselect1">
        <label>
        <select name="type" id="type">
          <option value="Single">Single</option>
          <option value="Double">Double</option>
        </select>
        </label>
        <span class="selectRequiredMsg">Please select an item.</span></span> </td>
    </tr>
    <tr>
      <th scope="row">Floor:</th>
      <td><span id="spryselect2">
        <label>
        <select name="floor" id="floor">
          <option value="First">First</option>
          <option value="Second">Second</option>
          <option value="Third">Third</option>
        </select>
        </label>
        <span class="selectRequiredMsg">Please select an item.</span></span> </td>
    </tr>
    <tr>
      <th scope="row">Price:</th>
      <td><span id="sprytextfield1">
        <label>
        <input name="price" type="text" id="price" size="10" maxlength="3" />
        </label>
        <span class="textfieldRequiredMsg">A value is required.</span></span> </td>
    </tr>
    <tr>
      <th scope="row">TV:</th>
      <td><span id="sprycheckbox1">
        <label></label>
        <span class="checkboxRequiredMsg">Please make a selection.</span></span> <label>
        <input type="checkbox" name="TV" id="TV" />
        </label></td>
    </tr>
    <tr>
      <th scope="row">Air-condition:</th>
      <td><span id="sprycheckbox2">
        <label></label>
        <span class="checkboxRequiredMsg">Please make a selection</span></span> <label>
        <input type="checkbox" name="air_condition" id="air_condition" />
        </label></td>
    </tr>
    <tr>
      <th scope="row">Internet access:</th>
      <td><span id="sprycheckbox3">
        <label></label>
        <span class="checkboxRequiredMsg">Please make a selection.</span></span> <label>
        <input type="checkbox" name="internet_access" id="internet_access" />
        </label></td>
    </tr>
    <tr>
      <th scope="row"> </th>
      <td> </td>
    </tr>
    <tr>
      <th scope="row"> </th>
      <td><label>
        <input type="submit" name="add" id="add" value="add" />
        </label>
      </td>
    </tr>
                </table>
</form>
<script type="text/javascript">
<!--
var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1");
var spryselect2 = new Spry.Widget.ValidationSelect("spryselect2");
var spryselect1 = new Spry.Widget.ValidationSelect("spryselect1");

//-->
</script>
</body>
</html>

صفحة تشييك بيانات الغرفة وكذلك اضافة الغرفة الى قاعدة البيانات addroom_check.php

<?php
include 'config.php';

$type= $_POST['type']; 
$floor= $_POST['floor'];
$price= $_POST['price']; 
$TV= $_POST['TV'];
$air_condition= $_POST['air_condition']; 
$internet_access= $_POST['internet_access']; 


// connect to the server, mysql_error() gives a msg if connection fails
$connection= mysql_connect ($db_server, $db_user, $db_pass ) or die ('unable to connect to the server'.mysql_error());

// select the database
mysql_select_db ($db_name) or die ('unable to select the database' .mysql_error);

// select the table from the database
$insert= "INSERT INTO rooms (roomNo ,from ,to ,type ,floor ,price ,TV ,air_condition ,internet_access, booked ) 
VALUES ('', '', '', '$type', '$floor', '$price', '$TV', '$air_condition', '$internet_access', '')";
$do_insert= mysql_query ($insert) OR DIE ('unable to insert records: '.mysql_error());

echo $do_insert;

if ($do_insert_one == 1)
    {
echo 'The new room has been added';
	}
else
{
echo 'Error: unable to add a new room';
}

?>

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

<body>
</body>
</html>

وش الحل ؟؟

#2

أنت تستعمل reserved words كأسماء لأعمدتك: from & to. جرب وضعها بين حرفي backquote "`"

$insert= "INSERT INTO rooms (roomNo ,`from` ,`to` ,type ,floor ,price ,TV ,air_condition ,internet_access, booked ) ...

تم تعديل هذه المشاركة بواسطة herch في 25 أغسطس 2010 في 15:11

#3
herch كتب:

أنت تستعمل reserved words كأسماء لأعمدتك: from & to. جرب وضعها بين حرفي backquote "`"

$insert= "INSERT INTO rooms (roomNo ,`from` ,`to` ,type ,floor ,price ,TV ,air_condition ,internet_access, booked ) ...

الله يعطيك العافية اخوي herch

نعم عدلت الكود اخوي ولما حاولت اضيف غرفة طلع لي الخطأ

1Error: unable to add a new room

ثم تأكدت من الداتا بيز .. لقيته اضاف لي الغرفة بس حقل الـ roomNo وهي الـ primary key تركه بدون رقم!!! يعني فارغ !!

كذلك : حقول الـ TV, air_condition , internet_access وضع لي قيمتهم صفر مع اني اشرت على الـ checkbox اللي مفروض يخون القيمة 1 بدلاً من 0 ؟!

ثم جربت اضيف غرفة اخرى طلع لي الخطأ

unable to insert records: Duplicate entry '' for key 'PRIMARY'

???

post-67983-078838700 1282739358_thumb.jp

post-67983-020598800 1282739704_thumb.jp

المرفقات
txt2.jpgtxt1.jpg

تم تعديل هذه المشاركة بواسطة سيد الاحساس في 25 أغسطس 2010 في 15:35

#4

السلام عليكم ورحمة الله

هل الحقل roonNo من نمط int و مفعل عليه auto_increment أي يزداد تلقائياً ؟

إن كان كذلك جرب استبدال '' إلى null

كالتالي مثلا :

INSERT INTO rooms (roomNo ,from ,to ,type ,floor ,price ,TV ,air_condition ,internet_access, booked ) 
VALUES (null, '', '', '$type', '$floor', '$price', '$TV', '$air_condition', '$internet_access', '')

بالنسبة للمشكلة الثانية

أنت لم تضع قيمة للـ Checkbox حتى يدخلها إلى قاعدة البيانات

عليك أن تضع له قيمة كالتالي مثلا :

<input type="checkbox" name="internet_access" id="internet_access" value="1"  />

بتوفيق الله ..

Everything will be fine when we TALK LESS, DO MORE


#5
MoHaMMaD Pro كتب:

السلام عليكم ورحمة الله

هل الحقل roonNo من نمط int و مفعل عليه auto_increment أي يزداد تلقائياً ؟

إن كان كذلك جرب استبدال '' إلى null

كالتالي مثلا :

INSERT INTO rooms (roomNo ,from ,to ,type ,floor ,price ,TV ,air_condition ,internet_access, booked ) 
VALUES (null, '', '', '$type', '$floor', '$price', '$TV', '$air_condition', '$internet_access', '')

بالنسبة للمشكلة الثانية

أنت لم تضع قيمة للـ Checkbox حتى يدخلها إلى قاعدة البيانات

عليك أن تضع له قيمة كالتالي مثلا :

<input type="checkbox" name="internet_access" id="internet_access" value="1"  />

بتوفيق الله ..

رحم الله والديك اخوي MoHaMMaD Pro نعم تم التعديل .. والكود اشتغل

شكراً لك اخوي MoHaMMaD Pro وكذلك اخوي herch على تعاونكم ..

سؤال اخير :

بالنسبة للصورة المرفقة ، انا عملت استعراض للبيانات من الداتا بيز و أريد أن تكون checkbox غير مرئية invisible بحيث أن المستخدم لا يستطيع تغيير القيم تتغير checked or unchecked بل يتم استدعاء القيم مباشرة من الداتا بيز وتعرض فقط ..

1=checked

0= unchecked

هل لابد أن استعمل كود javascript لكي اضبط الحقول هذه أما لا؟؟

شكر الله لكم أجمعين ،،

post-67983-075912600 1282748457_thumb.jp

post-67983-005042000 1282748968_thumb.jp

المرفقات
txt3.jpgtxt4.jpg
#6

جرب بإضافة الخاصية disabled

<input type="checkbox" disabled="disabled" />
#7
herch كتب:

جرب بإضافة الخاصية disabled

<input type="checkbox" disabled="disabled" />

جربت الخاصية ونجحت فعلاً .. جداً سعيد :)

يعطيك العافية اخوي herch وبارك الله فيك وفي الجميع ..

#8

يرجى اختيار نوع مناسب للموضوع (سؤال في هذه الحالة وتحديد عدد النقاط المناسب) في المرات القادمة.

Do as I say, not as I do

We are Anonymous. We are Legion. We don't forgive. We don't forget

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

عدد الزوار حالياً

المتواجدون خلال آخر دقيقتين · يتحدّث كل ٣٠ ثانية

—الإجمالي—أعضاء مسجّلون—زوار بدون تسجيل

جارٍ التحقق من المتواجدين…