Categories > TinyButStrong general >

inserting into mysql database

The forum is closed. Please use Stack Overflow for submitting new questions. Use tags: tinybutstrong , opentbs
By: fourcs
Date: 2007-08-04
Time: 10:22

inserting into mysql database

Could someone please give me the basic script to insert values in a mysql table? I've done it but with a bunch of errors. Thanks.
By: TomH
Date: 2007-08-04
Time: 10:30

Re: inserting into mysql database

For us to give you specific help, please post the code you are using and the errors you are getting. Post both the php and the html template files.
By: fourcs
Date: 2007-08-04
Time: 16:19

Re: inserting into mysql database


OK. Here are the two files

file 1:
<?php

include_once('tbs_class.php') ;
include_once('tbs_plugin_html.php'); // Plug-in for selecting HTML items.

$TBS = new clsTinyButStrong ;
$TBS->LoadTemplate('tbs_us_examples_form2.html') ;

//$typelist = array('<other>'=>'-','Mister'=>'Mr','Madame'=>'Mme','Missis'=>'Ms') ;
//$TBS->MergeBlock('typeblk',$typelist) ;

if (!isset($_POST)) $_POST=&$HTTP_POST_VARS ;
if (!isset($_POST['x_type'])) { 
  $res_name = '' ;
  $res_score = '' ;
  $res_date  = '' ;
  $res_team  = '' ;
  $msg_text = 'Enter your information and click on [Validate].' ;
  $msg_color = '#0099CC' ; //blue
} else {
   $msg_text  = '' ;
    $res_name  = $_POST['res_name'] ;
    $res_score = $_POST['res_score'] ;
   $res_date  = $_POST['res_date'];
   $res_team  = $_POST['res_team'];
  if ((trim($res_name)=='-')   and ($msg_text=='')) $msg_text = 'Please enter your name.' ;
  if ((trim($res_score)=='')    and ($msg_text=='')) $msg_text = 'Please enter your score.' ;
  if ((trim($res_date)=='') and ($msg_text=='')) $msg_text = 'Please enter your date.' ;
  if ((trim($res_team)=='') and ($msg_text=='')) $msg_text = 'Please enter your team.' ;
  if ($msg_text=='') {
    $msg_text = 'Thank you.' ;
    $msg_color = '#336600' ; //green
    } else {
    $msg_color = '#990000' ; //red
    }
}

$TBS->Show() ;


// insert values in table
//$TBS = new clsTinyButStrong ;
$cnx_id = mysql_connect('localhost','admin','admin') ;
mysql_select_db('samp_db',$cnx_id) ;

$SQL ="INSERT INTO t_tbs_exemples values('res_id',$res_name,$res_score,$res_date,$res_team)" ;

mysql_close($cnx_id) ;
?>

file 2:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>TinyButStrong</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="tbs_us_examples_0styles.css" rel="stylesheet" type="text/css">
</head>
<body>
<p align="center" class="title-page">Example of getting data from a Form</p>
<p align="center">This example illustrates how to handle getting and retreiving data from a Form.</p>
<form action="[var..script_name]" method="post" name="frm_info" id="frm_info">
  <table width="300" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
      <td width="296"><table width="100%" border="1" cellpadding="0" cellspacing="0" bordercolor="0099CC">
          <tr>
            <td height="20" bgcolor="#00CCCC">Enter information:</td>
          </tr>
          <tr>
            <td><table width="100%" border="0" cellspacing="0" cellpadding="2">
                <tr align="left" valign="bottom" class="back-special2">
                  <td>Name: </td>
                  <td><input name="res_name" type="text" class="normal" id="x_name2" value="[var.res_name]">
                  </td>
                </tr>
                <tr align="left" valign="bottom" class="back-special2">
                  <td>Score:</td>
                  <td><input name="res_score" type="text" class="normal" id="x_subname2" value="[var.res_score]">
                  </td>
                </tr>
                <tr align="left" valign="bottom" class="back-special2">
                  <td>Date:</td>
                  <td><input name="res_date" type="text" class="normal" id="x_subname2" value="[var.res_date]">
                  </td>
                </tr>
                <tr align="left" valign="bottom" class="back-special2">
                  <td>Team:</td>
                  <td><input name="res_team" type="text" class="normal" id="x_subname2" value="[var.res_team]">
                  </td>
                </tr>
              </table></td>
          </tr>
          <tr>
            <td height="30" bgcolor="#00CCCC"><div align="center">
                <input name="btn_save" type="submit" class="normal" id="btn_save2" value="  Validate  ">
              </div></td>
          </tr>
        </table></td>
    </tr>
  </table>
  <div align="center"><br>
    <strong><font color="[var.msg_color]">[var.msg_text]</font></strong> </div>
</form>
</body>
</html>

By: TomH
Date: 2007-08-05
Time: 13:11

Re: inserting into mysql database

Okay, I see a little now...

(1) In TBS php file: noting happens after the $TBS->Show(); line - so that's a big part of the difficulty ;)

(2) You need to do your db INSERT/UPDATE before $TBS->Show()

(3) Best to do these db actions inside of your FORM processing logic.

For example, if you wanted to populate the FORM with the user data
(A) you do the SELECT first then do the TBS->MergeBlock
(B) after the user submits FORM and you have validated the data (the FORM $btn_save=="Validate" ) you do the db UPDATE (or INSERT if it is first time for the user)

Something like this, UNtested psuedo code...

if (!isset($_POST)) $_POST=&$HTTP_POST_VARS ;
if (!isset($_POST['x_type'])) {
  $res_name = '' ;
  $res_score = '' ;
  $res_date  = '' ;
  $res_team  = '' ;
  $msg_text = 'Enter your information and click on [Validate].' ;
  $msg_color = '#0099CC' ; //blue
} else {
   $msg_text  = '' ;
$res_name  = $_POST['res_name'] ;
$res_score = $_POST['res_score'] ;
   $res_date  = $_POST['res_date'];
   $res_team  = $_POST['res_team'];
  if ((trim($res_name)=='-')   and ($msg_text=='')) $msg_text = 'Please enter your name.' ;
  if ((trim($res_score)=='')    and ($msg_text=='')) $msg_text = 'Please enter your score.' ;
  if ((trim($res_date)=='') and ($msg_text=='')) $msg_text = 'Please enter your date.' ;
  if ((trim($res_team)=='') and ($msg_text=='')) $msg_text = 'Please enter your team.' ;
  if ($msg_text=='') {
// assuming this is where you know that the changes are valid and want to do the INSERT
$SQL ="INSERT INTO t_tbs_exemples values('res_id',$res_name,$res_score,$res_date,$res_team)" ;
mysql_query($SQL);

    $msg_text = 'Thank you.' ;
    $msg_color = '#336600' ; //green
} else {
    $msg_color = '#990000' ; //red
}
}

// get the current data for this user and populate the form

$cnx_id = mysql_connect('localhost','admin','admin') ;
mysql_select_db('samp_db',$cnx_id) ;

$SQL ="SELECT FROM t_tbs_exemples $res_name,$res_score,$res_date,$res_team WHERE res_id = $res_id " ;

$TBS->MergeBlock('blk1',$cnx_id, 'SELECT FROM t_tbs_exemples $res_name,$res_score,$res_date,$res_team WHERE res_id = $res_id ' ;

mysql_close($cnx_id) ; // put this just before the SHOW

$TBS->Show() ;


You'll have to fill in the details for getting the $res_id to get the data for the user -- but I think you'll get the idea for how to get the db INSERT/UPDATE done from the above.

I hope this gives you a place to get started from