+ Reply to Thread
Results 1 to 5 of 5

Thread: form to mysql

  1. #1
    xsystemx is offline x10Hosting Member xsystemx is an unknown quantity at this point
    Join Date
    Mar 2009
    Posts
    4

    form to mysql

    HTML Code:
    <script language='javascript'>
    function verifyMe(){
    var msg='';
    if(document.getElementById('Artist').value==''){
     msg+='- Artist\n\n';}
    if(document.getElementById('Album').value==''){
     msg+='- Album\n\n';}
    if(document.getElementById('Song').value==''){
     msg+='- Song\n\n';}
    if(!document.getElementById('Guitar[0]').checked && !document.getElementById('Guitar[1]').checked && !document.getElementById('Guitar[2]').checked && !document.getElementById('Guitar[3]').checked){
    msg+='- Guitar\n\n';}
    if(!document.getElementById('Bass[0]').checked && !document.getElementById('Bass[1]').checked && !document.getElementById('Bass[2]').checked && !document.getElementById('Bass[3]').checked){
    msg+='- Bass\n\n';}
    if(!document.getElementById('Drums[0]').checked && !document.getElementById('Drums[1]').checked && !document.getElementById('Drums[2]').checked && !document.getElementById('Drums[3]').checked){
    msg+='- Drums\n\n';}
    if(document.getElementById('Download').value==''){
     msg+='- Download\n\n';}
    if(msg!=''){
     alert('The following fields are empty or invalid:\n\n'+msg);
     return false
    }else{
     return true }
    }
    </script>
    <form name='SUBMIT' action='submit2.php' method='POST' enctype='application/x-www-form-urlencoded' onsubmit='return verifyMe();'>
    
    <table cellspacing='0' border="0" width="100%">
     <tr><td>
      <fieldset class="fieldset">
        <legend>Artist</legend>
        <table cellpadding="0" cellspacing="3" border="0" width="100%">
        <tr>
         <td>
          <input type='text' name='Artist' id='Artist' size='50' maxlength='50'  value=''>
         </td>
        </tr>
        </table>
      </fieldset>
     <td></tr>
     <tr><td>
      <fieldset class="fieldset">
        <legend>Album</legend>
        <table cellpadding="0" cellspacing="3" border="0" width="100%">
        <tr>
         <td>
          <input type='text' name='Album' id='Album' size='50' maxlength='50'  value=''>
         </td>
        </tr>
        </table>
      </fieldset>
     <td></tr>
     <tr><td>
      <fieldset class="fieldset">
        <legend>Song</legend>
        <table cellpadding="0" cellspacing="3" border="0" width="100%">
        <tr>
         <td>
          <input type='text' name='Song' id='Song' size='50' maxlength='50'  value=''>
         </td>
        </tr>
        </table>
      </fieldset>
     <td></tr>
     <tr><td>
      <fieldset class="fieldset">
        <legend>Guitar</legend>
        <table cellpadding="0" cellspacing="3" border="0" width="100%">
        <tr>
         <td>
          <LABEL ACCESSKEY=''><input type='checkbox' name='Guitar[0]' id='Guitar[0]' value='1'></LABEL>Expert<BR/>
          <LABEL ACCESSKEY=''><input type='checkbox' name='Guitar[1]' id='Guitar[1]' value='1'></LABEL>Hard<BR/>
          <LABEL ACCESSKEY=''><input type='checkbox' name='Guitar[2]' id='Guitar[2]' value='1'></LABEL>Medium<BR/>
          <LABEL ACCESSKEY=''><input type='checkbox' name='Guitar[3]' id='Guitar[3]' value='1'></LABEL>Easy<BR/>
         </td>
        </tr>
        </table>
      </fieldset>
     <td></tr>
     <tr><td>
      <fieldset class="fieldset">
        <legend>Bass</legend>
        <table cellpadding="0" cellspacing="3" border="0" width="100%">
        <tr>
         <td>
          <LABEL ACCESSKEY=''><input type='checkbox' name='Bass[0]' id='Bass[0]' value='1'></LABEL>Expert<BR/>
          <LABEL ACCESSKEY=''><input type='checkbox' name='Bass[1]' id='Bass[1]' value='1'></LABEL>Hard<BR/>
          <LABEL ACCESSKEY=''><input type='checkbox' name='Bass[2]' id='Bass[2]' value='1'></LABEL>Medium<BR/>
          <LABEL ACCESSKEY=''><input type='checkbox' name='Bass[3]' id='Bass[3]' value='1'></LABEL>Easy<BR/>
         </td>
        </tr>
        </table>
      </fieldset>
     <td></tr>
     <tr><td>
      <fieldset class="fieldset">
        <legend>Drums</legend>
        <table cellpadding="0" cellspacing="3" border="0" width="100%">
        <tr>
         <td>
          <LABEL ACCESSKEY=''><input type='checkbox' name='Drums[0]' id='Drums[0]' value='1'></LABEL>Expert<BR/>
          <LABEL ACCESSKEY=''><input type='checkbox' name='Drums[1]' id='Drums[1]' value='1'></LABEL>Hard<BR/>
          <LABEL ACCESSKEY=''><input type='checkbox' name='Drums[2]' id='Drums[2]' value='1'></LABEL>Medium<BR/>
          <LABEL ACCESSKEY=''><input type='checkbox' name='Drums[3]' id='Drums[3]' value='1'></LABEL>Easy<BR/>
         </td>
        </tr>
        </table>
      </fieldset>
     <td></tr>
     <tr><td>
      <fieldset class="fieldset">
        <legend>Download</legend>
        <table cellpadding="0" cellspacing="3" border="0" width="100%">
        <tr>
         <td>
          <input type='text' name='Download' id='Download' size='50' maxlength='500'  value='Download'>
         </td>
        </tr>
        </table>
      </fieldset>
     <td></tr>
     <tr><td align='right'>
      <fieldset class="fieldset">
        <legend></legend>
        <table cellpadding="0" cellspacing="3" border="0" width="100%">
        <tr>
         <td>
          <input type='submit' name='submit' value='Submit'>&nbsp;<input type='reset' name='reset' value='Reset'>
         </td>
        </tr>
        </table>
      </fieldset>
     <td></tr>
    </table>
    
    </form>
    <LABEL ACCESSKEY=''><input type='checkbox' name='Guitar[0]' id='Guitar[0]' value='1'></LABEL>Expert<BR/>
    <LABEL ACCESSKEY=''><input type='checkbox' name='Guitar[1]' id='Guitar[1]' value='1'></LABEL>Hard<BR/>
    <LABEL ACCESSKEY=''><input type='checkbox' name='Guitar[2]' id='Guitar[2]' value='1'></LABEL>Medium<BR/>
    <LABEL ACCESSKEY=''><input type='checkbox' name='Guitar[3]' id='Guitar[3]' value='1'></LABEL>Easy<BR/>
    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <meta name="keywords" content="" />
    <meta name="description" content="" />
    <title>FOF-DB</title>
    <link href="default.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
    <?php include("./include/1.php"); ?>
    <?php 
    include("./include/2.php"); ?>
    <div id="wrapper">
    <?php include("./include/3.php"); ?>
    <div id="col-two">
    <div class="boxed">
    <h2 class="title">STEP 3</h2>
    <div class="content">
    <p>
    <? 
    $A
    =$_POST['Artist']; 
    $B=$_POST['Album']; 
    $C=$_POST['Song'];
    $D=$_POST['Guitar[0]'];
    $E=$_POST['Guitar[1]'];
    $F=$_POST['Guitar[2]'];
    $G=$_POST['Guitar[3]'];
    $H=$_POST['Bass[0]'];
    $I=$_POST['Bass[1]'];
    $J=$_POST['Bass[2]'];
    $K=$_POST['Bass[3]'];
    $L=$_POST['Drums[0]'];
    $M=$_POST['Drums[1]'];
    $N=$_POST['Drums[2]'];
    $O=$_POST['Drums[3]'];
    $P=$_POST['Download'];
     
    mysql_connect("localhost""""") or die(mysql_error());
    mysql_select_db("") or die(mysql_error());
    mysql_query("INSERT INTO `ARTIST` (`id` ,`ARTIST`) VALUES (NULL , '$A');");
    mysql_query("INSERT INTO `ALBUM` (`ID` ,`ARTIST` ,`ALBUM` ,`YEAR` ,`ART` ) VALUES (NULL , '$A', '$B', '', '');");
    mysql_query("INSERT INTO `SONG` (`ID` ,`ARTIST` ,`ALBUM` ,`SONG` ,`DISC` ,`TIME` ,`TRACK` ,`CAREER` ,`TIER` ,`DOWNLOAD` ,`GX` ,`GH` ,`GM` ,`GE` ,`BX` ,`BH` ,`BM` ,`BE` ,`DX` ,`DH` ,`DM` ,`DE` )VALUES (NULL , '$A', '$B', '$C', 'NULL', 'NULL', 'NULL', NULL , NULL , '$P', '$D', '$E', '$F', '$G', '$H', '$I', '$J', '$K', '$L', '$M', '$N', '$O');"); 
    Print 
    "SONG HAS BEEN SUBMITED"
    ?> 
    </p>
    </div>
    </div>
    </div>
    <?php include("./include/4.php"); ?>
    </div>
    <?php include("./include/5.php"); ?>
    </body>
    </html>
    $D=$_POST['Guitar[0]'];
    $E=$_POST['Guitar[1]'];
    $F=$_POST['Guitar[2]'];
    $G=$_POST['Guitar[3]'];

    I want it to inset a 1 into the rows where a checkbox is checked and a 0 if not checked
    Last edited by xsystemx; 04-10-2009 at 10:46 PM.

  2. #2
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: form to mysql

    Almost there. You gave a minimal test case and said what you want it to do, but didn't say what it ends up doing (i.e. what the problem is).

    Quote Originally Posted by xsystemx View Post
    I want it to inset a 1 into the rows where a checkbox is checked and a 0 if not checked
    Looking at your insert statement for table SONG, the instruments and difficulty levels are stored in one row. Your current client-side form validation function ensures anyone using the form will have checked at least one field for each instrument. Therefore, a checkbox will always be checked when your form handler runs. Do you mean you want to insert the check value in the column?

    Looking over your code, there are a few errors that could be causing whatever the problem is that you didn't mention, as well as some other areas that could use improvement.

    The form handler is covered first as it contains errors (the form page just needs improvements).
    PHP Code:
    <?php include("./include/1.php"); ?> 
    <?php include("./include/2.php"); ?> 
    <div id="wrapper"> 
    <?php include("./include/3.php"); ?> 
    <div id="col-two"> 
    <div class="boxed"> 
    <h2 class="title">STEP 3</h2> 
    <div class="content"> 
    <p> 
    <?  
    $A
    =$_POST['Artist'];
    Unless you're writing obfuscated code, use descriptive names. "1.php", "$A" &c are poor choices. It's much easier to keep track of what a script does when you use descriptive names, which is very important when you come back after 6 months (or years), by which time you've forgotten how your script works.
    PHP Code:
    $D=$_POST['Guitar[0]']; 
    When a form variable's name ends with square brackets, PHP will store it as an array. In your script, $_POST['Guitar'] is an array and $_POST['Guitar[0]'] isn't set. The way to access a particular element is (eg) $_POST['Guitar'][0].

    You don't appear to need to use numbers to indicate difficulty. Why not use words?

    PHP Code:
    mysql_query("INSERT INTO `ARTIST` (`id` ,`ARTIST`) VALUES (NULL , '$A');"); 
    NO! NO! NO! Never pass unfiltered user input to a subsystem, as you do with $A, $B &c. You just opened yourself to SQL injection. Use the filter functions to remove harmful data.

    On to the form page.
    Code:
    <script language='javascript'>
    function verifyMe(){
    var msg='';
    if(document.getElementById('Artist').value==''){
     msg+='- Artist\n\n';}
    ...
    if(!document.getElementById('Guitar[0]').checked && !document.getElementById('Guitar[1]').checked && !document.getElementById('Guitar[2]').checked && !document.getElementById('Guitar[3]').checked){
    msg+='- Guitar\n\n';}
    ...
    • Use the type="text/javascript" attribute for <script> elements, not language.
    • If you find yourself repeating code 3 or more times, you need a loop or a function.
      Code:
      <script type='text/javascript'>
      function verify(form){
        var msg='';
        for (field in {Artist: 1, Album: 1, Song: 1, Download: 1}) {
          if(form[field].value==''){
            msg+='- '+field+'\n';
          }
        }
        INSTRLOOP: for (field in {Guitar: 1, Bass: 1, Drums: 1}) {
          for (var i=0; i<4; ++i) {
              if (form[field+'['+i+']'].checked) continue INSTRLOOP;
          }
          msg += '- '+field+'\n';
        }
        if(msg!=''){
          alert('The following fields are empty or invalid:\n\n'+msg);
          return false;
        }else{
          return true;
        }
      }
    • Even better than just putting up an alert (which forces the user to keep in mind which fields need to be fixed), add a class to the invalid elements & display a message next to the invalid elements. In a style sheet, style the class to highlight the elements. When the user starts to edit an element, remove the class to clear the highlighting.

    HTML Code:
    <table cellspacing='0' border="0" width="100%">
     <tr><td>
       <fieldset class="fieldset">
    ...
        <legend>Guitar</legend>
        <table cellpadding="0" cellspacing="3" border="0" width="100%">
        <tr>
         <td>
          <LABEL ACCESSKEY=''><input type='checkbox' name='Guitar[0]' id='Guitar[0]' value='1'></LABEL>Expert<BR/>
          <LABEL ACCESSKEY=''><input type='checkbox' name='Guitar[1]' id='Guitar[1]' value='1'></LABEL>Hard<BR/>
          <LABEL ACCESSKEY=''><input type='checkbox' name='Guitar[2]' id='Guitar[2]' value='1'></LABEL>Medium<BR/>
          <LABEL ACCESSKEY=''><input type='checkbox' name='Guitar[3]' id='Guitar[3]' value='1'></LABEL>Easy<BR/>
         </td>
        </tr>
        </table>
    ...
    • If the form fields were in a single row, the outer table would be warranted because the form data represents a row of data. As it stands, the outer table is not justified. None of the inner tables will ever be justified.
    • The "fieldset" class is either redundant or poorly named. What are you using it for?
    • Fieldset are for, well, sets of fields. Use a <label> to label each field & drop the fieldsets.
    • <label> elements are meant to contain a text label for an input, not a form input.
    • Why include the accesskey attribute if you don't set it?
    • Ordering the difficulty fields from "Expert" to "Easy" is backwards. Reverse the order.

    Here's some sample code to illustrate the above suggestions:
    HTML Code:
    <style type="text/css">
    form ul, form li {
      margin: 0; padding: 0;
      list-style-type: none;
    }
    form .msg {
      display: none;
    }
    .invalid input {
      border: 1px solid red;
      background: #FCC;
    }
    form .invalid .msg {
      display: inline;
    }
    </style>
    <script type="text/javascript">
    function validate(form){
      for (field in {Artist: 1, Album: 1, Song: 1, Download: 1}) {
        if(form[field].value==''){
          form[field].nextSibling.lastChild.nodeValue="Missing value.";
          markInvalid(form[field]);
          msg+='- '+field+'\n';
        }
      }
      ...
      if (msg) {
        alert("You've forgotten to fill out some fields.  Please enter data in the highlighted fields and resubmit.\n\nThe following fields are empty or invalid:\n"+msg);
        return false;
      } else {
        return true;
      }
    }
    function markInvalid(elt) {
      elt.parentNode.className += 'invalid';
    }
    function clearInvalid(elt) {
      elt.parentNode.className = elt.parentNode.className.replace(/\binvalid\b/, '');
    }
    </script>
    <form onsubmit="validate(this);">
      <ul>
        <li><label for="Artist">Artist</label>
          <input name="Artist" size='50' maxlength='50' onkeypress="clearInvalid(this)"/><span class="msg"><img src="images/err"/>&nbsp;</span>
        </li>
        ...
      </ul>
    </form>
    The use of inline event listeners (onsubmit, onkeypress) is a quick and dirty approach. The modern way would be to use a JS library or addEventListener/attachEvent to register the event listener.

  3. #3
    Twinkie is offline Banned Twinkie is an unknown quantity at this point
    Join Date
    Sep 2007
    Location
    Ft. Lauderdale, Florida
    Posts
    1,389

    Re: form to mysql

    Wow, you better get some rep for this XD

  4. #4
    xsystemx is offline x10Hosting Member xsystemx is an unknown quantity at this point
    Join Date
    Mar 2009
    Posts
    4

    Re: form to mysql

    Looking at your insert statement for table SONG, the instruments and difficulty levels are stored in one row. Your current client-side form validation function ensures anyone using the form will have checked at least one field for each instrument. Therefore, a checkbox will always be checked when your form handler runs. Do you mean you want to insert the check value in the column?
    Yes that is exatyl wat i want, if its checked an INT, will be inserted, 1 if its checked and 0 if not, i will fix up my code with your suggesions and pust the resulting code

    The "fieldset" class is either redundant or poorly named. What are you using it for? its used to make to page look good, i just copyed a html source from a IPB reg page
    Why include the accesskey attribute if you don't set it? i uses phpformgen from a website and it put it thever wven if i didn';t specify one when i was greating the form
    i you need a preview fofdb.x10hosting.com/Submit.php
    i am not sure exatly on how to do the verifyMe thing u suggested

    HTML Code:
    <script type='text/javascript'>
    function verifyMe(){
    var msg='';
    
    if(document.getElementById('Artist').value==''){
    	msg+='- Artist\n\n';}
    
    if(document.getElementById('Album').value==''){
    	msg+='- Album\n\n';}
    
    if(document.getElementById('Song').value==''){
    	msg+='- Song\n\n';}
    
    if(!document.getElementById('Guitar[0]').checked && !document.getElementById('Guitar[1]').checked && !document.getElementById('Guitar[2]').checked && !document.getElementById('Guitar[3]').checked){
    msg+='- Guitar\n\n';}
    
    if(!document.getElementById('Bass[0]').checked && !document.getElementById('Bass[1]').checked && !document.getElementById('Bass[2]').checked && !document.getElementById('Bass[3]').checked){
    msg+='- Bass\n\n';}
    
    if(!document.getElementById('Drums[0]').checked && !document.getElementById('Drums[1]').checked && !document.getElementById('Drums[2]').checked && !document.getElementById('Drums[3]').checked){
    msg+='- Drums\n\n';}
    
    if(document.getElementById('Download').value==''){
    	msg+='- Download\n\n';}
    
    if(msg!=''){
    	alert('The following fields are empty or invalid:\n\n'+msg);
    	return false
    }else{
    	return true }
    
    }
    </script>
    <form name='SUBMIT' action='submit2.php' method='POST' enctype='application/x-www-form-urlencoded' onsubmit='return verifyMe();'>
    
    
    <table cellspacing='0' border="0" width="100%">
    	<tr><td>
    		<fieldset class="fieldset">
    				<legend>Artist</legend>
    				<table cellpadding="0" cellspacing="3" border="0" width="100%">
    				<tr>
    					<td>
    						<input type='text' name='Artist' id='Artist' size='50' maxlength='50'  value=''>
    					</td>
    				</tr>
    				</table>
    		</fieldset>
    	<td></tr>
    	<tr><td>
    		<fieldset class="fieldset">
    				<legend>Album</legend>
    				<table cellpadding="0" cellspacing="3" border="0" width="100%">
    				<tr>
    					<td>
    						<input type='text' name='Album' id='Album' size='50' maxlength='50'  value=''>
    					</td>
    				</tr>
    				</table>
    		</fieldset>
    	<td></tr>
    	<tr><td>
    		<fieldset class="fieldset">
    				<legend>Song</legend>
    				<table cellpadding="0" cellspacing="3" border="0" width="100%">
    				<tr>
    					<td>
    						<input type='text' name='Song' id='Song' size='50' maxlength='50'  value=''>
    					</td>
    				</tr>
    				</table>
    		</fieldset>
    	<td></tr>
    	<tr><td>
    		<fieldset class="fieldset">
    				<legend>Guitar</legend>
    				<table cellpadding="0" cellspacing="3" border="0" width="100%">
    				<tr>
    					<td>
    						<LABEL><input type='checkbox' name='GuitarX' id='GuitarX' value='1'></LABEL>Expert<BR/>
    						<LABEL><input type='checkbox' name='GuitarH' id='GuitarH' value='1'></LABEL>Hard<BR/>
    						<LABEL><input type='checkbox' name='GuitarM' id='GuitarM' value='1'></LABEL>Medium<BR/>
    						<LABEL><input type='checkbox' name='GuitarE' id='GuitarE' value='1'></LABEL>Easy<BR/>
    					</td>
    				</tr>
    				</table>
    		</fieldset>
    	<td></tr>
    	<tr><td>
    		<fieldset class="fieldset">
    				<legend>Bass</legend>
    				<table cellpadding="0" cellspacing="3" border="0" width="100%">
    				<tr>
    					<td>
    						<LABEL><input type='checkbox' name='BassX' id='BassX' value='1'></LABEL>Expert<BR/>
    						<LABEL><input type='checkbox' name='BassH' id='BassH' value='1'></LABEL>Hard<BR/>
    						<LABEL><input type='checkbox' name='BassM' id='BassM' value='1'></LABEL>Medium<BR/>
    						<LABEL><input type='checkbox' name='BassE' id='BassE' value='1'></LABEL>Easy<BR/>
    					</td>
    				</tr>
    				</table>
    		</fieldset>
    	<td></tr>
    	<tr><td>
    		<fieldset class="fieldset">
    				<legend>Drums</legend>
    				<table cellpadding="0" cellspacing="3" border="0" width="100%">
    				<tr>
    					<td>
    						<LABEL><input type='checkbox' name='DrumsX' id='DrumsX' value='1'></LABEL>Expert<BR/>
    						<LABEL><input type='checkbox' name='DrumsH' id='DrumsH' value='1'></LABEL>Hard<BR/>
    						<LABEL><input type='checkbox' name='DrumsM' id='DrumsM' value='1'></LABEL>Medium<BR/>
    						<LABEL><input type='checkbox' name='DrumsE' id='DrumsE' value='1'></LABEL>Easy<BR/>
    					</td>
    				</tr>
    				</table>
    		</fieldset>
    	<td></tr>
    	<tr><td>
    		<fieldset class="fieldset">
    				<legend>Download</legend>
    				<table cellpadding="0" cellspacing="3" border="0" width="100%">
    				<tr>
    					<td>
    						<input type='text' name='Download' id='Download' size='50' maxlength='500'  value='Download'>
    					</td>
    				</tr>
    				</table>
    		</fieldset>
    	<td></tr>
    	<tr><td align='right'>
    		<fieldset class="fieldset">
    				<legend></legend>
    				<table cellpadding="0" cellspacing="3" border="0" width="100%">
    				<tr>
    					<td>
    						<input type='submit' name='submit' value='Submit'>&nbsp;<input type='reset' name='reset' value='Reset'>
    					</td>
    				</tr>
    				</table>
    		</fieldset>
    	<td></tr>
    </table>
    
    
    </form>

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <meta name="keywords" content="" />
    <meta name="description" content="" />
    <title>FOF-DB</title>
    <link href="default.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
    <script type='text/javascript'>
    function no_injection($string){
    $string = htmlspecialchars($string);
    $string = trim($string);
    $string = stripslashes($string);
    $string = mysql_real_escape_string($string);
    return $string;
    }
    </script>
    <?php include("./include/1.php"); ?>
    <?php 
    include("./include/2.php"); ?>
    <div id="wrapper">
    <?php include("./include/3.php"); ?>

    <div id="col-two">
    <div class="boxed">
    <h2 class="title">SONG SUBMITED</h2>
    <div class="content">
    <p>

    <? 
    $A 
    no_injection($_POST['Artist']); 
    $B no_injection($_POST['Album']); 
    $C no_injection($_POST['Song']);
    $D no_injection($_POST['GuitarX']);
    $E no_injection($_POST['GuitarH']);
    $F no_injection($_POST['GuitarM']);
    $G no_injection($_POST['GuitarE']);
    $H no_injection($_POST['BassX']);
    $I no_injection($_POST['BassH']);
    $J no_injection($_POST['BassM']);
    $K no_injection($_POST['BassE']);
    $L no_injection($_POST['DrumsX']);
    $M no_injection($_POST['DrumsH']);
    $N no_injection($_POST['DrumsM']);
    $O no_injection($_POST['DrumsE']);
    $P no_injection($_POST['Download']);
     
    mysql_connect("localhost""""") or die(mysql_error());
    mysql_select_db("xsystemx_songs") or die(mysql_error());
    mysql_query("INSERT INTO `ARTIST`.`ARTIST` (`id` ,`ARTIST`) VALUES (NULL , '$A');");
    mysql_query("INSERT INTO `xsystemx_songs`.`ALBUM` (`ID` ,`ARTIST` ,`ALBUM` ,`YEAR` ,`ART` )VALUES (NULL , '$A', '$B', '', '');");
    mysql_query("INSERT INTO `xsystemx_songs`.`SONG` (`ID` ,`ARTIST` ,`ALBUM` ,`SONG` ,`DISC` ,`TIME` ,`TRACK` ,`CAREER` ,`TIER` ,`DOWNLOAD` ,`GX` ,`GH` ,`GM` ,`GE` ,`BX` ,`BH` ,`BM` ,`BE` ,`DX` ,`DH` ,`DM` ,`DE` )VALUES (NULL , '$A', '$B', '$C', 'NULL', 'NULL', 'NULL', NULL , NULL , '$P', '$D', '$E', '$F', '$G', '$H', '$I', '$J', '$K', '$L', '$M', '$N', '$O');"); 
    Print 
    "SONG HAS BEEN SUBMITED"
    ?> 
    </p>
    </div>
    </div>

    </div>
    <?php include("./include/4.php"); ?>
    </div>
    <?php include("./include/5.php"); ?>
    </body>
    </html>
    Last edited by xsystemx; 04-11-2009 at 03:03 PM.

  5. #5
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: form to mysql

    Quote Originally Posted by xsystemx View Post
    Yes that is exatyl wat i want, if its checked an INT, will be inserted, 1 if its checked and 0 if not, i will fix up my code with your suggesions and pust the resulting code
    Then it sounds like the problem is that the variables you want to insert aren't defined because browsers won't pass values for unchecked checkboxes. If PHP were more like Perl or Javascript, you could use $Guitar[hard] = $_REQUEST['Guitar']['hard'] || 0; (in Perl and JS, || returns the value of the first defined expression, whereas it's always boolean valued in PHP. Perl has an even better operator to set a default value: //). As it stands, you can do one of two things: set a variable if not already set (using isset for the test), or use array_merge(). In both cases, we first filter the user input:
    PHP Code:
    $passed filter_var_array($_REQUEST, array(
        
    'Download' => FILTER_SANITIZE_MAGIC_QUOTES,
        
    'Artist' => FILTER_SANITIZE_MAGIC_QUOTES,
        
    'Album' => FILTER_SANITIZE_MAGIC_QUOTES,
        
    'Song' => FILTER_SANITIZE_MAGIC_QUOTES,
        
    'Guitar' => array(
            
    'filter' => FILTER_SANITIZE_NUMBER_INT,
            
    'flags' => FILTER_REQUIRE_ARRAY,
            
    'options' => array('min_range' => 0'max_range' => 1)
        ),
        
    'Bass' => array(
            
    'filter' => FILTER_SANITIZE_NUMBER_INT,
            
    'flags' => FILTER_REQUIRE_ARRAY,
            
    'options' => array('min_range' => 0'max_range' => 1)
        ),
        
    'Drums' => array(
            
    'filter' => FILTER_SANITIZE_NUMBER_INT,
            
    'flags' => FILTER_REQUIRE_ARRAY,
            
    'options' => array('min_range' => 0'max_range' => 1)
        )
    )); 
    I named the variable "$passed" because I'm assuming the user is inputting the levels zhe has passed. If there's a better name for the data, use it for the variable name.

    FILTER_SANITIZE_MAGIC_QUOTES isn't the best choice for strings. Using PDO would be best, but it's not enabled under PHP Intermediate (I'm not certain about other versions or paid hosting). Between the two is mysqli::real_escape_string. As MySQL Improved is enabled under PHP Intermediate, I recommend using it instead of the basic MySQL driver.

    On to option 1: test & set
    PHP Code:
    foreach (array('Guitar''Bass''Drums') as $instrument) {
      foreach (array(
    'easy''medium''hard''expert') as $difficulty) {
        if (! isset(
    $passed[$instrument][difficulty])) {
          
    $passed[$instrument][difficulty] = 0;
    } } } 
    Option 2: array_merge()
    PHP Code:
    $difficulties=array('easy'=>0'medium'=>0'hard'=>0'expert'=>0);
    $passed array_merge(array('Guitar' => $difficulties
            
    'Bass' => $difficulties
            
    'Drums' => $difficulties), 
        
    $passed
    ); 

    Quote Originally Posted by xsystemx View Post
    i uses phpformgen from a website and it put it thever wven if i didn';t specify one when i was greating the form
    Web design code generators can be easy to use but mostly range from terrible to mediocre in quality, which can produce problems later on in development. It's much better to learn how to do it yourself.

    Quote Originally Posted by xsystemx View Post
    i am not sure exatly on how to do the verifyMe thing u suggested
    Look at the sample code I posted that includes the 'validate()' function.

+ Reply to Thread

Similar Threads

  1. [PHP] MySQL and PHP
    By Bryon in forum Tutorials
    Replies: 43
    Last Post: 03-24-2011, 07:27 AM
  2. Which is better postgresql or MySQL?
    By Kurai Kumo in forum Scripts & 3rd Party Apps
    Replies: 11
    Last Post: 12-10-2009, 08:17 AM
  3. Replies: 14
    Last Post: 09-29-2008, 07:07 PM
  4. New Site-Suggestions?
    By mnoutside in forum Review My Site
    Replies: 9
    Last Post: 08-27-2008, 07:01 AM
  5. Have a problem with my forum
    By tikloos in forum Scripts & 3rd Party Apps
    Replies: 43
    Last Post: 01-19-2006, 01:14 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers