I am trying to create a table in the database. If the table already exists, I want Ajax to leave a message to say that this table already exists.
This is what I have so far:
JS
<script>
// Add table
$(document).ready(function() {
$("#sender_table").find(".add-icon").click(function() {
var tableName = $(this).attr("data-tablename");
$.ajax({
type: 'post',
url: "<?= base_url(); ?>table/moveTable/",
data: "table_name=" + $(this).attr("data-tablename"),
success: function(r) {
$.notify(tableName + " was successfully added.", "success");
}
});
});
});
</script>
PHP
public function moveTable() {
$this->load->model('Connection_model');
$sTablename = $this->input->post('table_name', true);
$db1 = $this->session->userdata('receiver_db');
$db2 = $this->session->userdata('sender_db');
// Clone table
$this->Connection_model->get_custom_db('receiver')->query("CREATE TABLE $db1.$sTablename LIKE $db2.$sTablename");
// Copy data
$query = $this->Connection_model->get_custom_db('sender')->get($sTablename);
foreach ($query->result() as $row) {
$this->Connection_model->get_custom_db('receiver')->insert($sTablename, $row);
}
// Check if table exists
if ($this->Connection_model->get_custom_db('receiver')->table_exists($sTablename)) {
return true;
}
else {
return false;
}
}
Currently the message is always "tablename was successfully added", even when the table already exists.