Adding to my DB

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Aaron
Forum Commoner
Posts: 82
Joined: Sun May 12, 2002 2:51 pm

Adding to my DB

Post by Aaron »

Ok- I have a nice little table set up, a display page and such...

Now what I need is a script that will put the info into the rows.

The rows are as followed :

id, series, episode, title, info, date, author.


Obviously I'd need a form that would allow me to input the values for the next row, but i dont know how to make it and stuff.

Password protection wouldnt need building in because I've got a CGI script to lock the page.

To view the script in BETA action (looks shabby i know) go here!
User avatar
gotDNS
Forum Contributor
Posts: 217
Joined: Tue May 07, 2002 5:53 pm
Location: West Chester, PA

Post by gotDNS »

here is a generous snippet of some of my code:

The signup page!:

Code: Select all

<?php
session_start();

$connect = mysql_connect("localhost:3306", "techy") && mysql_select_db("forum")
or $failed = "Could not connect to database.";

echo "<html><head>
<title>Techy Board</title>
<link rel="stylesheet" type="text/css" href="forum.css" />
</head>
<body marginheight="0" marginwith="0" topmargin="0" leftmargin="0">";

if(session_is_registered("loggedin"))
&#123;
        echo "<img src="forumtop.gif" /><br />&nbsp;<b>You are logged in as $loggedin</b> : <a href="logout.php" class="NLINK">Logout</a><br />&nbsp;ERROR: You already seem to be signed up.<img src="smile.gif" />";
&#125;
else
&#123;
        echo "<img src="forumtop.gif" /><br /><br />
<table cellpadding="0" cellspacing="0" width="92%" border="0" align="center">
<tr>
<td align="left" width="80">
<b><u>Sign-Up</u></b><br /><br />
</td>
<td align="left"></td>
</tr>
<tr>
<td align="right" width="80">
<form action="signup2.php" method="post">
First Name:
</td>
<td align="left">
<input type="text" size="16" maxlength="24" name="fname" />
</td>
</tr>
<tr>
<td align="right" width="80">
Last Name:
</td>
<td align="left">
<input type="text" size="16" maxlength="24" name="lname" />
</td>
</tr>
<tr>
<td align="right" width="80">
Username:
</td>
<td align="left">
<input type="text" size="16" maxlength="16" name="usrname" />
</td>
</tr>
<tr>
<td align="right" width="80">
Password:
</td>
<td align="left">
<input type="password" size="16" maxlength="32" name="passwd" />
</td>
</tr>
<tr>
<td align="right" width="80">
Password2:
</td>
<td align="left">
<input type="password" size="16" maxlength="32" name="passwd2" /> (re-type)
</td>
</tr>
<tr>
<td align="right" width="80">
E-mail:
</td>
<td align="left">
<input type="text" size="16" maxlength="40" name="email" />
</td>
</tr>
<tr>
<td align="right" width="80" valign="top">Signature:</td>
<td align="left"><textarea name="sig" rows="10" cols="45"></textarea>
</td>
</tr>
<tr>
<td></td>
<td align="left"><input type="Submit" value="Sign-Up" class="BUTTON" /> <input type="reset" value="Reset" class="BUTTON" /></td>
</form>
</tr>
<tr>
<td></td>
<td><br /><a href="forum.php" class="NLINK">< Back to the forums</a></td>
</tr>
</table>";
&#125;
?>

</body>
</html>
The submit-to-db page!:

Code: Select all

<?php
session_start();

$connect = mysql_connect("localhost:3306", "techy") && mysql_select_db("forum")
or $failed = "Could not connect to database.";

$loginform = "<form method="post" action="login.php">
<img src="forumtop.gif" /><br />&nbsp;<b>Thank you for signing up!</b><br /><br />&nbsp;<b><u>Login</u></b><br />
&nbsp;Username: <input type="text" size="16" maxlength="16" name="username" />
 Password: <input type="password" size="16" maxlength="16" name="password" />
<input type="hidden" name="login" value="true" />
<input type="submit" value="Login" />
</form>";

if($fname != "" && $lname != "" && $usrname != "" && $passwd != "" && $passwd2 != "" && $email != "" && $passwd == $passwd2) &#123; 
$passwdon = md5($passwd);
mysql_query("insert into usrinfo set fname="$fname",lname="$lname",username="$usrname",password="$passwdon",email="$email",signature="$sig""); echo $loginform; &#125;
else &#123; $error = "ERROR:"; &#125;
?>

<html>
<head>
<title>Techy Board</title>
<link rel="stylesheet" type="text/css" href="forum.css" />
</head>
<body marginheight="0" marginwith="0" topmargin="0" leftmargin="0">
<?php if($error) &#123; echo "<img src="forumtop.gif" /><br /><br />&nbsp;<b>$error</b>"; &#125;

if($fname == "" || $lname == "" || $usrname == "" || $passwd == "" || $passwd2 == "" || $email == "") &#123; echo " <code>You did not complete the form, please hit your back button and complete it.</code>"; &#125;

if($passwd != $passwd2 || $passwd == "" || $passwd2 == "") &#123; echo " <code>Your passwords did not match, please hit your back button and re-type them.</code>"; &#125;
?>
</body>
</html>
Aaron
Forum Commoner
Posts: 82
Joined: Sun May 12, 2002 2:51 pm

Post by Aaron »

:| omg
Aaron
Forum Commoner
Posts: 82
Joined: Sun May 12, 2002 2:51 pm

Post by Aaron »

ok its not as hard as it looks :)

Only problem im havin is figuring out what bits art choosing the table ids, is it the || $lname == "" || $usrname == "" || $passwd == "" || $passwd2 == "" || $email ==

bits? ie i should change lname to id for it to write in that row?
Aaron
Forum Commoner
Posts: 82
Joined: Sun May 12, 2002 2:51 pm

Post by Aaron »

parse errors, deleted :S
User avatar
gotDNS
Forum Contributor
Posts: 217
Joined: Tue May 07, 2002 5:53 pm
Location: West Chester, PA

Post by gotDNS »

yes, change the variable name, etc.


parse errors in my code?....works for me :D

:S ?
Aaron
Forum Commoner
Posts: 82
Joined: Sun May 12, 2002 2:51 pm

Post by Aaron »

I changed it and it <span style='color:blue' title='I&#39;m naughty, are you naughty?'>smurf</span> up :S i suck :(
User avatar
gotDNS
Forum Contributor
Posts: 217
Joined: Tue May 07, 2002 5:53 pm
Location: West Chester, PA

Post by gotDNS »

hmmm, what did u change?

Paste the code and i'll take a look.
Aaron
Forum Commoner
Posts: 82
Joined: Sun May 12, 2002 2:51 pm

Post by Aaron »

Last edited by Aaron on Wed Jun 05, 2002 8:26 am, edited 1 time in total.
Aaron
Forum Commoner
Posts: 82
Joined: Sun May 12, 2002 2:51 pm

Post by Aaron »

OK proplem solved thx to Dave, from Evil Walrus.

Here it is for anyone interested!

Inputting!

Code: Select all

<?php 
session_start(); 

mysql_connect("localhost","user","password") or die(mysql_error());
mysql_select_db("db") or die(mysql_error());

echo "<html><head> 
<title>Techy Board</title> 
<link rel="stylesheet" type="text/css" href="forum.css" /> 
</head> 
<body marginheight="0" marginwith="0" topmargin="0" leftmargin="0">"; 

if(session_is_registered("loggedin")) 
&#123; 
        echo "<img src="forumtop.gif" /><br />&nbsp;<b>You are logged in as $loggedin</b> : <a href="logout.php" class="NLINK">Logout</a><br />&nbsp;ERROR: You already seem to be signed up.<img src="smile.gif" />"; 
&#125; 
else 
&#123; 
        echo "<img src="forumtop.gif" /><br /><br /> 
<table cellpadding="0" cellspacing="0" width="92%" border="0" align="center">
  <tr> 
    <td align="left" width="80"> <b><u>Sign-Up</u></b><br />
      <br />
    </td>
    <td align="left"></td>
  </tr>
  <tr> 
    <td align="right" width="80"> <form action="submit.php" method="post"> 
      author: </td>
    <td align="left"> 
      <input type="text" size="16" maxlength="24" name="author" />
    </td>
  </tr>
  <tr> 
    <td align="right" width="80">&nbsp;</td>
    <td align="left">&nbsp;</td>
  </tr>
  <tr> 
    <td align="right" width="80"> series</td>
    <td align="left"> 
      <input type="text" size="16" maxlength="16" name="series" />
    </td>
  </tr>
  <tr> 
    <td align="right" width="80"> episode</td>
    <td align="left"> 
      <input type="text" size="16" maxlength="32" name="episode" />
    </td>
  </tr>
  <tr> 
    <td align="right" width="80"> title</td>
    <td align="left"> 
      <input type="text" size="16" maxlength="32" name="title" />
    </td>
  </tr>
  <tr> 
    <td align="right" width="80"> info</td>
    <td align="left"> 
      <textarea name="info" rows="10" cols="45"></textarea>
    </td>
  </tr>
  <tr> 
    <td align="right" width="80" valign="top">date</td>
    <td align="left">
      <input type="text" size="16" maxlength="40" name="date" />
    </td>
  </tr>
  <tr> 
    <td></td>
    <td align="left">
      <input type="Submit" value="Sign-Up" class="BUTTON" />
      <input type="reset" value="Reset" class="BUTTON" />
    </td></form>
  </tr>
  <tr> 
    <td></td>
    <td><br />
    </td>
  </tr>
</table>
"; 
&#125; 
?> 
</body> 
</html>
Finalising

Code: Select all

<?php 
session_start(); 

mysql_connect("localhost","user","password") or die(mysql_error());
mysql_select_db("db") or die(mysql_error());

if($author  != ""&& $series != "" && $episode != "" && $title != "" && $info != "") &#123; 
mysql_query("INSERT INTO unknownz.unz VALUES ('','$series','$episode','$title','$info','$date','$author')") or die(mysql_error()); echo $loginform; &#125; 
else &#123; $error = "<b> - Error - </b>"; &#125; 
?>

<html> 
<head> 
<title>Series Submitter</title> 
</head> 
<body marginheight="0" marginwith="0" topmargin="0" leftmargin="0"> 
<?php if($error) &#123; echo "<b> - Error - </b>"; &#125; 

if($author  == "" || $series == "" || $episode == "" || $title == "" || $info == "") &#123; echo " <code>You did not complete the form, please hit your back button and complete it.</code>"; &#125; 


?> 
</body> 
</html>
If anyone has any further ideas for it?

Anywho, next problem =/ I was reading a tutorial on Ascenvia about deleting certain ids, tried it and it didnt work, have a look :

Code: Select all

<form method="get" action="file.php"> //file is the name of this file
<?
mysql_pconnect("localhost","unknownz","Cm784Xhti"); //connect to the mySQL
mysql_select_db("unz"); //select the db
if(!$submit)//if submit has not already been pressed
&#123;
    $result = mysql_query("select * from news order by id"); //select all the news
    while($r=mysql_fetch_array($result)) //run the while loop
    &#123; 
        $title=$r&#1111;"title"];//take out the title
        $id=$r&#1111;"id"];//take out the id
        ?>
        <INPUT TYPE="RADIO" NAME="id" VALUE="<?php echo $id;?>"><br> //the radio button with the value of each id
        <?php echo $id;?><br> //output the id
        <?php echo $title?><br> //output the title
<?
    &#125;?> //end of the while loop
<input type="submit" value="submit" name="Submit"></form> //submit
<?
&#125;?>
[/u][/quote]
Post Reply