הוסטס - פורום אחסון האתרים הגדול בישראל

הוסטס - פורום אחסון האתרים הגדול בישראל (https://hosts.co.il/forums/index.php)
-   פורום תיכנות (https://hosts.co.il/forums/forumdisplay.php?f=14)
-   -   PHP | איך אני עושה בדיקה אם יש ערך מסויים במסד? (https://hosts.co.il/forums/showthread.php?t=59628)

0xfo7d 03-02-08 13:58

PHP | איך אני עושה בדיקה אם יש ערך מסויים במסד?
 
שלום.
אני בונה רשימת תפוצה ב PHP.
אני מעוניין לעשות דבר כזה...אם משתמש מנסה להוסיף כתובת דוא"ל...שתהיה בדיקה אם הדוא"ל כבר קיים במערכת....אם כן...שיוציא הודעה "כתובת זו כבר קיימת במערכת", אם לא...שיוסיף למסד..
זה הקטע קוד הרלוונטי:

PHP קוד:

       mysql_query("INSERT INTO `emails` SET
        `email_address`='"
.$_POST["email_address"]."'
        "
);
       echo 
mysql_error();
       if(
mysql_affected_rows() > 0) {
          echo 
"<font color='red'>האימייל נוסף בהצלחה</font>";
          echo 
"<META HTTP-EQUIV=Refresh CONTENT='1; URL=?action=mail_manager&sa=edit'>";
       } else {
          echo 
"<font color='red'>שגיאה בעת תהליך הוספת האימייל</font>";
       } 

מישהו יכול לעזור לי עם זה?

תודה רבה,
מתן.

Shon12 03-02-08 14:21

נסה את זה:
HTML קוד:

$result = mysql_query("INSERT INTO `emails` (email_address) VALUES ($_POST['email_address'])") or die(mysql_error());
$search = mysql_num_rows($result);
        if ($search == 0)
        {
        echo("<font color=\"red\">האימייל נוסף בהצלחה</font>");
        }
        else {
        echo("<font color=\"red\">אנא בחר אימייל אחר</font>");
        }


0xfo7d 03-02-08 14:55

זה אומר שיש שגיאה בשורה הזו:
PHP קוד:

$result mysql_query("INSERT INTO `emails` (email_address) VALUES ($_POST['email_address'])") or die(mysql_error()); 

זה אומר את זה ברגע שנכנסים לדף...לא כאשר אני מנסה להוסיף.


זו השגיאה:
קוד:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/ventilated/domains/viewp.net/public_html/mysite/admin/admin_mailing.inc.php on line 130

Shon12 03-02-08 15:24

נסה את זה:
HTML קוד:

$email = $_POST['email_address'];

$result = mysql_query("INSERT INTO `emails` (email_address) VALUES ('$email')") or die(mysql_error());
$search = mysql_num_rows($result);
        if ($search == 0)
        {
        echo("<font color=\"red\">האימייל נוסף בהצלחה</font>");
        }
        else {
        echo("<font color=\"red\">אנא בחר אימייל אחר</font>");
        }


WaReZ 03-02-08 15:31

קוד:

$result = mysql_query("INSERT INTO `emails` (`email_address`) VALUES ('".$_POST['email_address']."')") or die(mysql_error());

0xfo7d 03-02-08 15:36

א. זה מוציא שגיאה:

קוד:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/ventilated/domains/viewp.net/public_html/mysite/admin/admin_mailing.inc.php on line 133
ב.זה מוסיף את המייל למסד(מייל שכבר קיים).


שורה 133:
PHP קוד:

$search mysql_num_rows($result); 


-roee- 03-02-08 15:38

mysql_num_rows אפשר לעשות רק לשאילתת SELECT
כמו כן, שים לב שאתה קודם שם את האימייל שאתה בודק במסד ורק אז בודק אם הוא קיים - אז בטוח שהוא יהיה קיים.

Ron | CSite.co.il 03-02-08 15:40

PHP קוד:

$email $_POST['email_address'];

$result mysql_query("SELECT * FROM `emails` WHERE `email_address`= '$email')") or die(mysql_error());
$search mysql_num_rows($result);
    if (
$search == 0)
    {
        
mysql_query("INSERT INTO `emails` (`email_address`) VALUES ('$email')") or die(mysql_error());
    echo(
"<font color=\"red\">האימייל נוסף בהצלחה</font>");
    }
    else {
    echo(
"<font color=\"red\">אנא בחר אימייל אחר</font>");
    } 

יש מצב קטן שיש טעויות קטנות אבל לא נראה לי ,כי אני חייב ללכת ועשיתי את זה מהר.
תהנה.

0xfo7d 03-02-08 15:41

ציטוט:

נכתב במקור על ידי roeenoy (פרסם 605901)
mysql_num_rows אפשר לעשות רק לשאילתת SELECT
כמו כן, שים לב שאתה קודם שם את האימייל שאתה בודק במסד ורק אז בודק אם הוא קיים - אז בטוח שהוא יהיה קיים.

תוכל לנסות לעזור לי לשלב את זה נכון בקוד???

וברור שקודם הוספתי את המייל ולאחר מכן ניסיתי שוב...

0xfo7d 03-02-08 15:44

ציטוט:

נכתב במקור על ידי Ron-Pro (פרסם 605902)
PHP קוד:

$email $_POST['email_address'];

$result mysql_query("SELECT * FROM `emails` WHERE `email_address`= '$email')") or die(mysql_error());
$search mysql_num_rows($result);
    if (
$search == 0)
    {
        
mysql_query("INSERT INTO `emails` (`email_address`) VALUES ('$email')") or die(mysql_error());
    echo(
"<font color=\"red\">האימייל נוסף בהצלחה</font>");
    }
    else {
    echo(
"<font color=\"red\">אנא בחר אימייל אחר</font>");
    } 

יש מצב קטן שיש טעויות קטנות אבל לא נראה לי ,כי אני חייב ללכת ועשיתי את זה מהר.
תהנה.




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 ')' at line 1

pirsomet 03-02-08 15:47

0xfo7d
קוד:

mysql_query("INSERT INTO `emails` SET
        `email_address`='".$_POST["email_address"]."'
        ");
      echo mysql_error();
      if(mysql_affected_rows() > 0) {
          echo "<font color='red'>האימייל נוסף בהצלחה</font>";
          echo "<META HTTP-EQUIV=Refresh CONTENT='1; URL=?action=mail_manager&sa=edit'>";
      } else {
          echo "<font color='red'>שגיאה בעת תהליך הוספת האימייל</font>";
      }

הדרך שבה אתה בודק היתה יכולה להיות נכונה רק אם היית מגדיר את email_address כ UNIQUE KEY במוסד הנתונים

ou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1

תבדוק עכשיו
קוד:


$email = $_POST['email_address'];

$result = mysql_query("SELECT * FROM `emails` WHERE `email_address`= '$email' ") or die(mysql_error());
$search = mysql_num_rows($result);
    if ($search == 0)
    {
        mysql_query("INSERT INTO `emails` (`email_address`) VALUES ('$email')") or die(mysql_error());
    echo("<font color=\"red\">האימייל נוסף בהצלחה</font>");
    }
    else {
    echo("<font color=\"red\">אנא בחר אימייל אחר</font>");
    }


0xfo7d 03-02-08 16:18

ציטוט:

נכתב במקור על ידי pirsomet (פרסם 605913)
0xfo7d
קוד:

mysql_query("INSERT INTO `emails` SET
        `email_address`='".$_POST["email_address"]."'
        ");
      echo mysql_error();
      if(mysql_affected_rows() > 0) {
          echo "<font color='red'>האימייל נוסף בהצלחה</font>";
          echo "<META HTTP-EQUIV=Refresh CONTENT='1; URL=?action=mail_manager&sa=edit'>";
      } else {
          echo "<font color='red'>שגיאה בעת תהליך הוספת האימייל</font>";
      }

הדרך שבה אתה בודק היתה יכולה להיות נכונה רק אם היית מגדיר את email_address כ UNIQUE KEY במוסד הנתונים

ou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1

תבדוק עכשיו
קוד:


$email = $_POST['email_address'];

$result = mysql_query("SELECT * FROM `emails` WHERE `email_address`= '$email' ") or die(mysql_error());
$search = mysql_num_rows($result);
    if ($search == 0)
    {
        mysql_query("INSERT INTO `emails` (`email_address`) VALUES ('$email')") or die(mysql_error());
    echo("<font color=\"red\">האימייל נוסף בהצלחה</font>");
    }
    else {
    echo("<font color=\"red\">אנא בחר אימייל אחר</font>");
    }


תודה רבה רבה, זה עובד:)

AlmogBaku 03-02-08 16:56

טיפ קטן:

mysql_num_rows פחות בריא... הוא מיועד ל"אם כבר בדקת"...

יותר נכון לעשות:
PHP קוד:

SELECT count(*) as `num_rowsFROM `ALMOG_BAKUWHERE `email`='aa@aa.cc'

ואז למשוך את השדה 'num_rows', ככה בעצם מנוע ה SQL מחשב את מספר השורות, ולא עבודה כפולה[גם SQL שולף הכל-מיותר, וגם הPHP סופר].

Shon12 03-02-08 17:37

ציטוט:

נכתב במקור על ידי Baku (פרסם 605955)
טיפ קטן:

mysql_num_rows פחות בריא... הוא מיועד ל"אם כבר בדקת"...

יותר נכון לעשות:
PHP קוד:

SELECT count(*) as `num_rowsFROM `ALMOG_BAKUWHERE `email`='aa@aa.cc'

ואז למשוך את השדה 'num_rows', ככה בעצם מנוע ה SQL מחשב את מספר השורות, ולא עבודה כפולה[גם SQL שולף הכל-מיותר, וגם הPHP סופר].

לא הבנתי איך אפשר למשל לבדוק ככה אם האימייל קיים,
אפשר דוגמא קטנה?
כי הרי לפי מה שהבנתי הוא רק סופר כמה אימליים יש נגיד "נרשמו" 10 אימיילים אז הוא התוצאה של השאילתא שהראית תיהיה 10.

pirsomet 03-02-08 18:06

ציטוט:

נכתב במקור על ידי Shon12 (פרסם 605968)
לא הבנתי איך אפשר למשל לבדוק ככה אם האימייל קיים,
אפשר דוגמא קטנה?
כי הרי לפי מה שהבנתי הוא רק סופר כמה אימליים יש נגיד "נרשמו" 10 אימיילים אז הוא התוצאה של השאילתא שהראית תיהיה 10.

אם יש לפחות תוצאה אחד אז המאיל קיים .דוגמה:

קוד:

$sql="SELECT count(*) as `num_rows` FROM `ALMOG_BAKU` WHERE `email`='aa@aa.cc' ";
$result=mysql_query($sql);
$count=mysql_result($result,0,0);
if($coumt==0)
{
echo "everything ok";
}
else
{
echo "there such email";
}


Shon12 04-02-08 15:26

ציטוט:

נכתב במקור על ידי pirsomet (פרסם 605977)
אם יש לפחות תוצאה אחד אז המאיל קיים .דוגמה:

קוד:

$sql="SELECT count(*) as `num_rows` FROM `ALMOG_BAKU` WHERE `email`='aa@aa.cc' ";
$result=mysql_query($sql);
$count=mysql_result($result,0,0);
if($coumt==0)
{
echo "everything ok";
}
else
{
echo "there such email";
}


לא ממש הבנתי את הקוד.
מישהו אוליי יכול לסביר לי שורה שורה.
תודה


כל הזמנים הם GMT +2. הזמן כעת הוא 06:01.

מופעל באמצעות VBulletin גרסה 3.8.6
כל הזכויות שמורות ©
כל הזכויות שמורות לסולל יבוא ורשתות (1997) בע"מ