By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,216 Members | 1,013 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,216 IT Pros & Developers. It's quick & easy.

I can't store in database from my form

elen
P: 4
hi!
I wish for your help! I have a form and i want to store the results to my database and show me back what i sent to database,but my php script won't work.Can somebody help me ?I'm new in php

This is my html code for a form:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <title>ΦΟΡΜΑ ΣΤΟΙΧΕΙΩΝ ΤΑΥΤΟΤΗΤΑΣ ΑΣΘΕΝΟΥΣ</title>
  4. </head>
  5. <body bgcolor='fffaaa'>
  6. <form action="result_insert_stoixeia_tayt.php",method=post>
  7.  
  8. <table border=10 bgcolor=#aaafff align=center>
  9.  
  10. <tr bgcolor=#aaafff>
  11.   <td width=200> Επώνυμο συζύγου</td>
  12.   <td align=center ><input type="text" name="eponumsyzygval" size=20 maxlength=20></td>
  13. </tr>
  14.  
  15.  
  16. <tr bgcolor=#aaafff>
  17.   <td width=200> Επώνυμο πατρός</td>
  18.   <td align=center ><input type="text" name="eponumpatrval" size=20 maxlength=20></td>
  19. </tr>
  20.  
  21.  
  22. <tr bgcolor=#aaafff>
  23.   <td width=200> Όνομα</td>
  24.   <td align=center ><input type="text" name="onomval" size=20 maxlength=20></td>
  25. </tr>
  26.  
  27.  
  28.  
  29. <tr bgcolor=#aaafff>
  30.   <td width=200> Όνομα συζύγου</td>
  31.   <td align=center ><input type="text" name="onomsyzygval" size=20 maxlength=20></td>
  32. </tr>
  33.  
  34.  
  35.  
  36.  
  37. <tr bgcolor=#aaafff>
  38.   <td width=200> Όνομα πατρός</td>
  39.   <td align=center ><input type="text" name="onompatrval" size=20 maxlength=20></td>
  40. </tr>
  41.  
  42.  
  43.  
  44.  
  45.  
  46. <tr bgcolor=#aaafff>
  47.   <td width=200> Επάγγελμα</td>
  48.   <td align=center ><input type="text" name="epaggelval" size=20 maxlength=20></td>
  49. </tr>
  50.  
  51.  
  52.  
  53.  
  54.  
  55. <tr bgcolor=#aaafff>
  56.   <td width=200> Επάγγελμα συζύγου</td>
  57.   <td align=center ><input type="text" name="epaggelsyzygval" size=20 maxlength=20></td>
  58. </tr>
  59.  
  60.  
  61.  
  62.  
  63.  
  64. <tr bgcolor=#aaafff>
  65.   <td width=200> Ημερ/νία γέννησης</td>
  66.   <td align=center ><input type="text" name="imergenval" size=20 maxlength=20></td>
  67. </tr>
  68.  
  69.  
  70.  
  71.  
  72. <tr bgcolor=#aaafff>
  73.   <td width=200> Τόπος καταγωγής</td>
  74.   <td align=center ><input type="text" name="toposkatagval" size=20 maxlength=20></td>
  75. </tr>
  76.  
  77.  
  78.  
  79.  
  80.  
  81. <tr bgcolor=#aaafff>
  82.   <td width=200> Ασφαλιστικός Οργανισμός</td>
  83.   <td align=center ><input type="text" name="asfalistorgval" size=20 maxlength=20></td>
  84. </tr>
  85.  
  86.  
  87.  
  88.  
  89. <tr bgcolor=#aaafff>
  90.   <td width=200> Αρ.Αστυν.Ταυτότητας</td>
  91.   <td align=center ><input type="text" name="ap_ast_tayt" size=20 maxlength=20></td>
  92. </tr>
  93.  
  94.  
  95.  
  96.  
  97. <tr bgcolor=#aaafff>
  98.   <td width=200> Διεύθυνση</td>
  99.   <td align=center ><input type="date" name="dieythval" size=20 maxlength=20></td>
  100. </tr>
  101.  
  102.  
  103.  
  104.  
  105. <tr bgcolor=#aaafff>
  106.   <td width=200> Τηλέφωνο1</td>
  107.   <td align=center ><input type="text" name="til1val" size=20 maxlength=20></td>
  108. </tr>
  109.  
  110.  
  111. <tr bgcolor=#aaafff>
  112.   <td width=200> Τηλέφωνο2</td>
  113.   <td align=center ><input type="text" name="til2val" size=20 maxlength=20></td>
  114. </tr>
  115.  
  116.  
  117. <tr bgcolor=#aaafff>
  118.   <td colspan=2 align=center><input type=submit name=submit value="Καταχώρηση στοιχείων ταυτότητας"></td>
  119. </tr>
  120. </table>
  121. </form>
  122. </body>
  123. </html>
  124.  
  125. and this is the php script result_insert_stoixeia_tayt.php :
  126.  
  127.  
  128.  
  129. <?php
  130. $db = mysql_connect("127.0.0.1", "root");
  131. mysql_select_db("eleni");
  132.  
  133.  
  134.  
  135.  
  136.  
  137.   $sqlp = mysql_query("SELECT * FROM asthenis where ap_ast_tayt='$_POST[ap_ast_tayt]'");
  138.   $res = mysql_num_rows($sqlp);
  139.  
  140.  
  141. if (($_POST["eponumsyzygval"]!="")&&($_POST["onomval"]!="")&&($_POST["artaytval"]!=""))
  142. {
  143.  
  144.   if($res==0)
  145.   {
  146.      $sql = mysql_query("INSERT INTO asthenis VALUES ('$_POST[eponumsyzygval]', '$_POST[eponumpatrval]', '$_POST[onomval]', '$_POST[onomsyzygval]', '$_POST[onompatrval]', '$_POST[epaggelval]', '$_POST[epaggelsyzygval]', '$_POST[imergenval]', '$_POST[toposkatagval]', '$_POST[asfalistorgval]', '$_POST[ap_ast_tayt]', '$_POST[dieythval]', '$_POST[til1val]', '$_POST[til2val]')");
  147.  
  148.      $sql = mysql_query("SELECT * FROM asthenis WHERE ap_ast_tayt='$_POST[ap_ast_tayt]'");
  149.      $r = mysql_num_rows($sql);
  150.      if($r==0)
  151.        echo "<p>Η καταχώρηση σας δεν έγινε λόγω λάθους δεδομένων.Παρακαλώ προσπαθήστε ξανά<br>";
  152.      else
  153.      {
  154.        echo "<b><h2>Πραγματοποιήσατε επιτυχή εισαγωγή των στοιχείων της ασθενούς στη Βάση Δεδομένων</h2></b>";
  155.        echo "<br>";
  156.        echo " <b>Επώνυμο συζύγου:      </b>".$_POST["eponumsyzygval"]."<br>";
  157.        echo " <b>Επώνυμο πατρός:      </b>".$_POST["eponumpatrval"]."<br>";
  158.        echo " <b>Όνομα:      </b>".$_POST["onomval"]."<br>";
  159.        echo " <b>Όνομα συζύγου:      </b>".$_POST["onomsyzygval"]."<br>";
  160.        echo " <b>Όνομα πατρός:      </b>".$_POST["onompatrval"]."<br>";
  161.        echo " <b>Επάγγελμα:      </b>".$_POST["epaggelval"]."<br>";
  162.        echo " <b>Επάγγελμα συζύγου:      </b>".$_POST["epaggelsyzygval"]."<br>";
  163.        echo " <b>Ημερ/νία γέννησης:      </b>".$_POST["imergenval"]."<br>";
  164.        echo " <b>Τόπος καταγωγής:      </b>".$_POST["toposkatagval"]."<br>";
  165.        echo " <b>Ασφαλιστικός οργανισμός:      </b>".$_POST["asfalistorgval"]."<br>";
  166.        echo " <b>Αρ.Αστυν.Ταυτότητας:      </b>".$_POST["ap_ast_tayt"]."<br>";
  167.        echo " <b>Διεύθυνση:      </b>".$_POST["dieythval"]."<br>";
  168.        echo " <b>Τηλέφωνο1:      </b>".$_POST["til1val"]."<br>";
  169.        echo " <b>Τηλέφωνο2:      </b>".$_POST["til2val"]."<br>";
  170.     }
  171.   }
  172.   else
  173.     echo "<p>Υπάρχει ήδη καταχώρηση με αυτόν τον αριθμό ταυτότητας ";
  174. }
  175. else
  176.  
  177.     echo"<p>Τα πεδία επώνυμο συζύγου,όνομα ασθενούς και αριθμός αστυνομικής ταυτότητας δεν πρέπει να είναι κενά ";
  178.  
  179. ?>
  180.  
and this is the database and the table:
Expand|Select|Wrap|Line Numbers
  1. create database 'eleni';
  2. connect eleni;
  3. create table asthenis(
  4.        eponimo_sizigou varchar(20) not null,
  5.        eponimo_patros varchar(20) ,
  6.        onoma varchar(20) not null,
  7.        onoma_sizigou varchar(20) ,
  8.        onoma_patros varchar(20) ,
  9.        epaggelma varchar(20) ,
  10.        epaggelma_sizigou varchar(20) ,
  11.        hmerominia_gennisis date,
  12.        topos_katagogis varchar(20) ,
  13.        asfalistikos_org varchar(20) ,
  14.        ap_ast_tayt varchar(10) not null,
  15.        dieythinsi varchar(20) ,
  16.        tilefwno1 int(20),
  17.        tilefwno2 int(20),
  18.        primary key (ap_ast_tayt));
  19.  
Sep 9 '08 #1
Share this Question
Share on Google+
4 Replies


Atli
Expert 5K+
P: 5,058
Hi. Welcome to Bytes!

Exactly how is the code not working?
What isn't it doing that it should be doing?
What is it doing that it shouldn't be doing.

Are you getting any error messages?
How you tried to turn on the debug messages?

Also, use [code] tags when posting code examples.
Please read the Posting Guidelines before posting.

Thank you
Sep 9 '08 #2

chelvan
P: 90
why you not trying this syntax for the insert
Expand|Select|Wrap|Line Numbers
  1. $sql_ins=mysql_query("INSERT INTO [table_name] [inserting field names] VALUES ([inserting value])");
  2.  
table_name=your table name;
inserting field names=table colums;
inserting value=your variable;

this above kind syntax worked.
but i'm not sure about your syntax.


regards
chel-1
Sep 10 '08 #3

nine72
P: 21
This works for me each time so have stuck with it....


ADD TO YOUR FORM

Expand|Select|Wrap|Line Numbers
  1. <?PHP
  2.     // before your session_start()
  3.     require_once('Connections/connect_to_your_db.php');
  4.     require_once('db_insert/insert_form_data.php');
  5. ?>
  6.  
  7. <form method="POST" name="name_of_form" action="<?php echo $editFormAction; ?>">
  8. <input type="hidden" name="MM_insert" value="name_of_form">
  9. </form>
  10.  


Create the include_once file insert_form_data.php...
[PHP]
<?PHP

session_start();

require_once('../../Connections/your_db_connection_page.php');

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "name_of_form")) {
$insertSQL = sprintf("INSERT INTO fe (eponumsyzygval, eponumpatrval, onomval, onomsyzygval, onompatrval, epaggelval, epaggelsyzygval, imergenval, toposkatagval, asfalistorgval, ap_ast_tayt, dieythval, til1val, til2val) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
GetSQLValueString($_POST['eponumsyzygval'], "text"),
GetSQLValueString($_POST['eponumpatrval'], "text"),
GetSQLValueString($_POST['onomval'], "text"),
GetSQLValueString($_POST['onomsyzygval'], "text"),
GetSQLValueString($_POST['onompatrval'], "text"),
GetSQLValueString($_POST['epaggelval'], "text"),
GetSQLValueString($_POST['epaggelsyzygval'], "text"),
GetSQLValueString($_POST['imergenval'], "text"),
GetSQLValueString($_POST['toposkatagval'], "text"),
GetSQLValueString($_POST['asfalistorgval'], "text"),
GetSQLValueString($_POST['ap_ast_tayt'], "text"),
GetSQLValueString($_POST['dieythval'], "text"),
GetSQLValueString($_POST['til1val'], "text"),
GetSQLValueString($_POST['til2val'], "text")
);

mysql_select_db($database_yourdb, $yourdb);
$Result1 = mysql_query($insertSQL, $yourdb) or die(mysql_error());

$insertGoTo = "page_to_go_to_after_insert.php"; //redirect page name
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}

?>
[/PHP]

I know it is long, but has never faild to do just what I am asking it to do....

nine72
Sep 10 '08 #4

elen
P: 4
thank you nine 72 for your help :-)
Sep 14 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.