Buy Now
Sign in

Some database tables not updating URGENT!

SwingeyP , January 18 2021, 21:50
S
SwingeyP 46
January 18 2021, 21:50 #395703
I am updating an external database. Everything seems ok except for one field in this case 'SiteName'
I have a bit of PHP which collects the site name dependant on the Joomla User login
<?php

// Get the Joomla user info
$user =& JFactory::getUser();
$name = '';
$email = '';
$readonly = '';
if ( $user->id > 0 ) {
$ID = $user->id;
$UserName = $user->username;
$Name = $user->name;
$readonly = "readonly='readonly'";
}
//echo $ID. "<br>";

//Setup the LFT database connection (LFT_DATA not the Joomla instance)
$servername = "localhost";
$username = "LFT_DataUser";
$password = "[Ur1dirm3p]";
$dbname = "LFT_DATA";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT JoomlaID, SiteID, SiteName FROM SiteDetails WHERE JoomlaID=$ID"; // where Joomla UserID = SiteID or something like that.
$result = $conn->query($sql);

if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$JoomlaID = $row["JoomlaID"];
$SiteName = $row["SiteName"];
$SiteID = $row["SiteID"];
// echo "Joomla ID = " . $JoomlaID . "<br>";
}
} else {
echo "This is not a recognised user!";
}
$conn->close();
?>

<div class="ccms_form_element cfdiv_text" id="name_container_div" style="">
<label for="SiteName"><b>Site Name</b></label>
<input id="SiteName" maxlength="150" size="30" class=""
title="" value=" <?php echo $SiteName; ?>" name="SiteName"
type="text">
<div class="clear"></div>
<div id="error-message-name"></div>
</div>

<div class="ccms_form_element cfdiv_text" id="name_container_div" style="">
<label for="SiteID"><b>Site Id</b></label>
<input id="SiteID" maxlength="150" size="30" class=""
title="" value=" <?php echo $SiteID; ?>" name="SiteID"
type="text" <?php echo $readonly; ?> />
<div class="clear"></div>
<div id="error-message-name"></div>
</div>

In the above code the SiteID is entered to the database (to be fair I have no idea why. All I can assume is that as the variable is named it's part of the data collection for the 'SaveData' somewhere along the line - I haven't exclusively added it as with all the other form elements). However! SiteName is NOT written. Can anyone explain why?
Alternatively how do I add the SiteName to a form field. I haven't been able to fathom this out using the short codes. There must be a way of doing it? That way I can add the SaveData in the Data Behaviours like the other form elements.
admin 32
January 19 2021, 14:26 #395709
 Answer
The "Save Data" has a "Data sources" advanced setting, you can name your table field and set the value as {var:site_name}
In your PHP code, use this code to set the value of the var:
$this->set("site_name", 123);
S
SwingeyP 46
January 19 2021, 14:36 #395710
Woo hoo!- It worked Thank you so much. {var...] doh!
I have tried to mark this as an answer but I get error in your forum.
Thank you so much.