PHP-GTK2 Cookbook Forum
Welcome, Guest
Please Login or Register.    Lost Password?
Re:Cannot write text to table (1 viewing) (1) Guest
Go to bottom Post Reply Favoured: 0
TOPIC: Re:Cannot write text to table


#5874
cwjs (User)
Fresh Boarder
Posts: 6
graphgraph
User Offline Click here to see the profile of this user
Cannot write text to table 1 Year, 5 Months ago Karma: 0  
I am new to Joomla but I have used sql or rather Oracle before.
I have been playing with this for a couple of days now and I cannot see what I am doing wrong.
I am using Joomla 3 and directphp to connect to a table in an article.
This form runs OK and if I only use numbers it writes them to the table, but I want to use text, when I do it does not work.
I have tried varchar and char as the <type= but that does not work either, any ideas please


<form name="form2" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
derr:<input type="int" name="val0">
Name:<input type="text" name="val1"><br><br>
A1:<input type="int" name="val2">
Distance1:<input type="int" name="val3">
Distancemeasure1:<input type="text" name="val4">
T1:<input type="int" name="val5"><br><br>
A2:<input type="int" name="val6">
Distance2:<input type="int" name="val7">
Distancemeasure2:<input type="text" name="val8">
T2:<input type="int" name="val9"><br><br>
A3:<input type="int" name="val10">
Distance3:<input type="int" name="val11">
Distancemeasure3:<input type="text" name="val12">
T3:<input type="int" name="val13"><br><br>
A4:<input type="int" name="val14">
Distance4:<input type="int" name="val15">
Distancemeasure4:<input type="text" name="val16">
T4:<input type="int" name="val17"><br><br><br><br>
Outdoors:<input type="text" name="val18">
Zone:<input type="text" name="val19">
Tot:<input type="int" name="val20"><br><br><br><br>
<input type="submit" name="submit" value="Submit Form"><br>
</form>
<?php
$sfield0=$_POST['val0'];
$sfield1=$_POST['val1'];
$sfield2=$_POST['val2'];
$sfield3=$_POST['val3'];
$sfield4=$_POST['val4'];
$sfield5=$_POST['val5'];
$sfield6=$_POST['val6'];
$sfield7=$_POST['val7'];
$sfield8=$_POST['val8'];
$sfield9=$_POST['val9'];
$sfield10=$_POST['val10'];
$sfield11=$_POST['val11'];
$sfield12=$_POST['val12'];
$sfield13=$_POST['val13'];
$sfield14=$_POST['val14'];
$sfield15=$_POST['val15'];
$sfield16=$_POST['val16'];
$sfield17=$_POST['val17'];
$sfield18=$_POST['val18'];
$sfield19=$_POST['val19'];
$sfield20=$_POST['val20'];

$con=mysqli_connect("localhost","myname","password","databasename");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = "INSERT INTO tab1 (`code` , `name` , `a1` , `distance1` , `distancemeasure1` , `t1` , `a2` , `distance2` , `distancemeasure2` , `t2` , `a3` , `distance3` , `distancemeasure3` , `t3` , `a4` , `distance4` , `distancemeasure4` , `t4` , `outdoors` , `zone` , `tot` ) VALUES ($sfield0,$sfield1, $sfield2, $sfield3, $sfield4, $sfield5, $sfield6, $sfield7, $sfield8, $sfield9, $sfield10, $sfield11, $sfield12, $sfield13, $sfield14, $sfield15, $sfield16, $sfield17, $sfield18, $sfield19, $sfield20)";
mysql_query($query);
mysql_close();
?>
 
  The administrator has disabled public write access.

#5875
kksou (Admin)
Admin
Posts: 1722
graph
User Online Now Click here to see the profile of this user
Re:Cannot write text to table 1 Year, 5 Months ago Karma: 27  
Hi,

You need to do something like:
$query = "INSERT INTO tab1 (`code` , `name` , `a1`) VALUES ('$sfield0', '$sfield1', '$sfield2', '$sfield3')";

For mysql, if you don't quote in single quote, it will assume it's numbers.

Also, if you using DirectPHP, it means your code above is running within a Joomla article. If you set a mysql connection to another table, ALWAYS remember to switch the mysql connection back to the mysql database that's running your Joomla site. Otherwise you will find that your Joomla site will not be working after your mysql update. If you are using the same database, then it's fine.

Regards,
/kksou
 
  The administrator has disabled public write access.

#5876
cwjs (User)
Fresh Boarder
Posts: 6
graphgraph
User Offline Click here to see the profile of this user
Re:Cannot write text to table 1 Year, 5 Months ago Karma: 0  
Thank you, I was getting really stressed with that and it still seems odd to me to put quotes around a variable, but who am I.
I am using tables that are in my database so I do not appear to have connection problems, is there a way to avoid putting in

$con=mysqli_connect("localhost","myname","password","databasename");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
.
.
.
mysql_close();
}


each time I want to use a different table or the same one in a different article
 
  The administrator has disabled public write access.
#5879
kksou (Admin)
Admin
Posts: 1722
graph
User Online Now Click here to see the profile of this user
Re:Cannot write text to table 1 Year, 5 Months ago Karma: 27  
Hi,

Just like standard PHP, you can put all these into a function such as init_db(). Then every time you need to make a connection, just do a init_db();

Regards,
/kksou
 
  The administrator has disabled public write access.
#5880
cwjs (User)
Fresh Boarder
Posts: 6
graphgraph
User Offline Click here to see the profile of this user
Re:Cannot write text to table 1 Year, 5 Months ago Karma: 0  
Thanks kksou,
Really helpful

Regards
Clive
 
  The administrator has disabled public write access.



Go to top Post Reply
Powered by FireBoardget the latest posts directly to your desktop

Blog - Forum - Privacy Policy - Contact Us
Copyright © 2006-2012. kksou.com. All Rights Reserved