Jump to content

Basic Question


star_commander

Recommended Posts

I am a beginner programmer and I'm learning PHP on my own. I'm trying to use MySql to store text and and call it back based on an id number the user creates. I have two scripts, the first one inserts the data, the second one takes it out based on the id number.

<?php$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");mysql_select_db($db) or die ("Unable to select database!");$memo = $_POST['memos'];$id = $_POST['ids'];mysql_query("INSERT INTO test (id, memo) VALUES ('$ids', '$memos')") or die ("Query Failed");echo "Success";?>

<?php$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");mysql_select_db($db) or die ("Unable to select database!");$id = $_POST['id'];mysql_select_db("my_db", $con);$result = mysql_query("SELECT * FROM testWHERE id='"$id"'");while($row = mysql_fetch_array($result))  {  echo $row['id'] . " " . $row['memo'];  echo "<br />";  }  ?>

Thanks

Link to comment
Share on other sites

I have a text box for users to input text. Below that is another box to input a number. When they click submit, the variables are stored in the database. There is also another forum with a field where they can input their "id" number they set earlier and pull up their message. This is just a script I am making to learn PHP and Mysql. :)

Link to comment
Share on other sites

in the first code u use $ids in the query, while before that u say $id = $_POST['ids'], not $idsin the 2nd code u use $con as second parameter in mysel_select_db instead of $connection

Link to comment
Share on other sites

The logic is fine. Just a bit of a syntax problem:

$result = mysql_query("SELECT * FROM testWHERE id='$id'");

Link to comment
Share on other sites

Thanks. I'm still having problems, but I think they might have to do with the way I set up the databases. I'm pretty sure the code is correct.

<?php$host = "localhost";$user = "test";$pass = "test";$db = "test";$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");mysql_select_db($db) or die ("Unable to select database!");$memo = $_POST['memo'];$id = $_POST['id'];mysql_query("INSERT INTO test (id, memo) VALUES ('$id', '$memo')") or die ("Query Failed");echo "Success";?>

<?php$host = "localhost";$user = "test";$pass = "test";$db = "test";$con = mysql_connect($host, $user, $pass) or die ("Unable to connect!");mysql_select_db($db) or die ("Unable to select database!");$id = $_POST['id'];mysql_select_db("my_db", $con);$result = mysql_query("SELECT * FROM testWHERE id='$id'");while($row = mysql_fetch_array($result))  {  echo $row['id'] . " " . $row['memo'];  echo "<br />";  }  ?>

Link to comment
Share on other sites

The code is working. :) Just when I insert a value through the form, it creates a record, but the record is blank. No data is inserted into the database, but the record is created. If I manually enter information into the records through phpmyadmin then the second script works. Does anyone know what is causing this?

Link to comment
Share on other sites

Are you sure your form is being sent through POST and that it has two fields named "memo" and "id"?

Link to comment
Share on other sites

Your second PHP code is missing ?> at the end. I don't know if ther's stuff after that or whatnot, but if that's supposed to be the end, might want to fix that. As Synook said, seeing your HTML might help to make sure it's using the things the PHP is trying to get it with.

Link to comment
Share on other sites

Here's the submit page, where the forms are.

<!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><style type="text/css"><!--.style1 {font-family: Arial, Helvetica, sans-serif}.style3 {	font-family: Arial, Helvetica, sans-serif;	font-size: 36px;	color: #000000;}--></style></head><!-- InstanceBegin template="/Templates/templateforpro.dwt" codeOutsideHTMLIsLocked="false" --><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><!-- InstanceBeginEditable name="doctitle" --><!-- InstanceEndEditable --><!-- InstanceBeginEditable name="head" --><!-- InstanceEndEditable --><style type="text/css"><!--body {	background-color: #000000;}.style1 {color: #FFFFFF}.center {}--></style><script src="Scripts/AC_RunActiveContent.js" type="text/javascript"></script></head><body><table width="987" height="625" border="0" align="center" cellpadding="0">  <tr>    <td height="139" colspan="2" valign="top" bgcolor="#FFFFFF" class="style1"><table width="979" height="132" border="0" cellpadding="0">      <tr>        <td width="174" height="126"><img src="images/CSlogo.gif" alt="" width="167" height="124" /></td>        <td width="787" height="126"><div align="center"><img src="images/banner.gif" alt="" width="547" height="138" /></div></td>      </tr>          </table></td>  </tr>  <!-- InstanceBeginEditable name="Region2" -->  <tr>    <td width="183" valign="top" bgcolor="#666666"><p>      <script type="text/javascript">AC_FL_RunContent( 'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0','width','183','height','67','src','ButtonH','quality','high','pluginspage','http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash','movie','ButtonH' ); //end AC code</script><noscript><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="183" height="67">        <param name="movie" value="ButtonH.swf" />        <param name="quality" value="high" />        <embed src="ButtonH.swf" quality="high" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="183" height="67"></embed>      </object></noscript>    </p>      <p>        <script type="text/javascript">AC_FL_RunContent( 'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0','width','183','height','67','src','ButtonA','quality','high','pluginspage','http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash','movie','ButtonA' ); //end AC code</script><noscript><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="183" height="67">          <param name="movie" value="ButtonA.swf" />          <param name="quality" value="high" />          <embed src="ButtonA.swf" quality="high" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="183" height="67"></embed>        </object></noscript>      </p>    <p>      <script type="text/javascript">AC_FL_RunContent( 'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0','width','183','height','67','src','ButtonS','quality','high','pluginspage','http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash','movie','ButtonS' ); //end AC code</script><noscript><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="183" height="67">        <param name="movie" value="ButtonS.swf" />        <param name="quality" value="high" />        <embed src="ButtonS.swf" quality="high" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="183" height="67"></embed>      </object></noscript>    </p>    <p>      <script type="text/javascript">AC_FL_RunContent( 'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0','width','183','height','67','src','ButtonB','quality','high','pluginspage','http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash','movie','ButtonB' ); //end AC code</script><noscript><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="183" height="67">        <param name="movie" value="ButtonB.swf" />        <param name="quality" value="high" />        <embed src="ButtonB.swf" quality="high" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="183" height="67"></embed>      </object></noscript>    </p>    <p>      <script type="text/javascript">AC_FL_RunContent( 'codebase','http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0','width','183','height','67','src','ButtonSour','quality','high','pluginspage','http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash','movie','ButtonSour' ); //end AC code</script>      <noscript><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="183" height="67">        <param name="movie" value="ButtonSour.swf" />        <param name="quality" value="high" />        <embed src="ButtonSour.swf" quality="high" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="183" height="67"></embed>      </object></noscript></p></td>    <td width="798" valign="top" bgcolor="#FFFFFF"><p> </p>    <p align="center"><span class="style3">About Us</span></p><p align="center">-----------------------------------------------------------------------------------------------------------</p>    <p align="center"><strong>Contact Us:</strong></p>    <p align="center">Use this form to send us your questions, comments, and suggestions. If you would like a response, please leave us your name and email.</p>    <form id="form1" name="form1" method="post" action="test3.php">      <label>              <div align="center">          <p>            <input type="text" name="memo" cols="75" rows="5" id="memo">          </p>          <p>            <label>ID:            <input name="id" type="text" id="id" size="6" />            </label>          </p>          <p>            <label>            <input type="submit" name="button" id="button" value="Submit" />            </label>            <br />          </p>        </div>  </label>      </form>        <p align="center">Have a message you want to see again? Type in the unique ID you recieved to view it agian.</p>    <form id="form2" name="form2" method="post" action="test4.php">      <label>        <div align="center">ID:          <input type="text" name="id" id="id" />          <br />          <input type="submit" name="button2" id="button2" value="Get" />        </div>      </label>    </form>    <p align="center"> </p></td>  </tr>  <!-- InstanceEndEditable --></table></body><!-- InstanceEnd --></html>

----------------------------------------------------------Nevermind, I took a look at the source for my submit page and found an error. It works fine now. Thanks.

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...