Jump to content

Posting data to mysql using a form


chaddsuk

Recommended Posts

Hi im probably going stupidly wrong with this but i cant seem to post data using the form below, the table shows ok and works fine i just cant add data to the table using the form.

 

am i doing something really stupid?

 

thanks

 

chris

 

 

<?php
     require "config.php";
?>


<form action="<?php mysql_query("INSERT INTO users (autoID, name)");?>" method="post">
name: <input type="text" name="name" />

<input type="submit" />
</form>





<?php

// Retrieve all the data from the "example" table
$result = mysql_query("SELECT name FROM users")
or die(mysql_error());  

// store the record of the "example" table into $row
$row = mysql_fetch_array( $result );
// Print out the contents of the entry 

      

// just some random data in an array
$age = "27";
$data = array('an','example','to','show','the','alternating','row','colors');
$rows = count($data);
// the two colors to switch between
$rowcolor1 = '#F0F0F2';
$rowcolor2 = '#FFFFFF';
// the background colors on mouseover
$hovercolor1 = '#BAD4EB';
$hovercolor2 = '#DCE9F4';


echo '
<center><table style="caption-side: top; 
border: 0.1em solid #eee;
border-collapse: collapse; 
margin: 1em; 
width: 30em;">
<caption style="font-weight: bold;">Demonstration of alternate row colors</caption>';
for($n = 0; $n < $rows; $n++)
{
// this is where the magic happens

			 if($n % 2 == 1)

			     {

			         // add more things to swop with each cycle

			         $style = $rowcolor1;

			         $hoverstyle = $hovercolor1;

		  }else{

			         $style = $rowcolor2; 

			         $hoverstyle = $hovercolor2;

			     }

			  

			     echo '

			    <tr id="row'.$n.'" style="background:'.$style.';"

			         onmouseover="this.style.background=\''.$hoverstyle.'\'"

			         onmouseout="this.style.background=\''.$style.'\'">

			         <td style="padding: 0.3em 1em;">'.$row[$n].'</td>

			     </tr>';

			 }

			  

			 echo '

			 </table></center>';

?>

 

Use

 tags - roopurt18[/b]
Link to comment
https://forums.phpfreaks.com/topic/87833-posting-data-to-mysql-using-a-form/
Share on other sites

Oh dear.

 

action="<?php mysql_query("INSERT INTO users (autoID, name)");?>" 

action is an HTML attribute that tells the browser which URL to submit the form to.  You do not put a call to mysql_query() as the action for a form.

 

Also, your MySQL statement is incorrect.

 

Try reading the tutorial that was linked to in the previous post.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.